Skip to content

Commit 03176a1

Browse files
committed
Instrument: fix the handling of C++ for-range loops
Change-Id: If2f61279b6ddee174354b30cc9a0636b4a8cb63b TN: V816-018
1 parent 0cb1e99 commit 03176a1

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

tools/gnatcov/clang-extensions.ads

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ package Clang.Extensions is
4545
function Get_Var_Init_Expr (C : Cursor_T) return Cursor_T
4646
with Import, Convention => C, External_Name => "clang_getVarInitExpr";
4747

48+
function Get_For_Range_Expr (C : Cursor_T) return Cursor_T
49+
with Import, Convention => C, External_Name => "clang_getForRangeExpr";
50+
4851
function Get_Then (C : Cursor_T) return Cursor_T
4952
with Import, Convention => C, External_Name => "clang_getThen";
5053

tools/gnatcov/clang-wrapper.cc

+19
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,31 @@ clang_getForInit (CXCursor C)
169169
{
170170
case Stmt::ForStmtClass:
171171
return MakeCXCursorWithNull (cast<ForStmt> (S)->getInit (), C);
172+
case Stmt::CXXForRangeStmtClass:
173+
return MakeCXCursorWithNull (cast<CXXForRangeStmt> (S)->getInit (),
174+
C);
172175
default:
173176
return clang_getNullCursor ();
174177
}
175178
return clang_getNullCursor ();
176179
}
177180

181+
extern "C" CXCursor
182+
clang_getForRangeExpr (CXCursor C)
183+
{
184+
const Stmt *stmt;
185+
const CXXForRangeStmt *for_stmt;
186+
187+
if (clang_isStatement (C.kind)
188+
&& (stmt = cxcursor::getCursorStmt (C))
189+
&& stmt->getStmtClass () == Stmt::CXXForRangeStmtClass)
190+
{
191+
for_stmt = cast<CXXForRangeStmt> (stmt);
192+
return MakeCXCursorWithNull (for_stmt->getRangeInit (), C);
193+
}
194+
return clang_getNullCursor ();
195+
}
196+
178197
extern "C" CXCursor
179198
clang_getForInc (CXCursor C)
180199
{

tools/gnatcov/instrument-c.adb

+20-1
Original file line numberDiff line numberDiff line change
@@ -1999,9 +1999,28 @@ package body Instrument.C is
19991999

20002000
when Cursor_CXX_For_Range_Stmt =>
20012001
declare
2002-
For_Body : constant Cursor_T := Get_Body (N);
2002+
For_Init_Stmt : constant Cursor_T := Get_For_Init (N);
2003+
For_Range_Decl : constant Cursor_T := Get_For_Range_Expr (N);
2004+
For_Body : constant Cursor_T := Get_Body (N);
20032005
begin
2006+
-- Generate SCO statements for both the init statement and
2007+
-- the range declaration initialization expression. Like all
2008+
-- statements, they can contain nested decisions.
2009+
2010+
Extend_Statement_Sequence
2011+
(For_Init_Stmt, ' ', Insertion_N => N);
2012+
Process_Decisions_Defer (For_Init_Stmt, 'X');
2013+
2014+
Extend_Statement_Sequence
2015+
(For_Range_Decl, ' ',
2016+
Insertion_N => For_Range_Decl,
2017+
Instr_Scheme => Instr_Expr);
2018+
Process_Decisions_Defer (For_Range_Decl, 'X');
2019+
20042020
Set_Statement_Entry;
2021+
2022+
-- Generate obligations for body statements
2023+
20052024
UIC.Pass.Curlify (N => For_Body, Rew => UIC.Rewriter);
20062025
Traverse_Statements
20072026
(IC, UIC,

0 commit comments

Comments
 (0)