Skip to content

Commit 9c2e770

Browse files
author
Yuanfang Chen
committed
Add begin source location for the attributed statement created from PragmaLoopHint decorated loop
Summary: Right now it is a '<invalid sloc>' for cases like this. CounterCoverageMappingBuilder relies on the information to decide the region for a attributed loop. Fixes PR40971 Reviewers: ABataev, jdenny, lebedev.ri, aaron.ballman Reviewed by: jdenny, aaron.ballman Differential Revision: https://reviews.llvm.org/D80944
1 parent 675cefb commit 9c2e770

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

clang/lib/Parse/ParsePragma.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,7 @@ void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
30993099
Token LoopHintTok;
31003100
LoopHintTok.startToken();
31013101
LoopHintTok.setKind(tok::annot_pragma_loop_hint);
3102-
LoopHintTok.setLocation(PragmaName.getLocation());
3102+
LoopHintTok.setLocation(Introducer.Loc);
31033103
LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
31043104
LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
31053105
TokenList.push_back(LoopHintTok);
@@ -3186,7 +3186,7 @@ void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
31863186
auto TokenArray = std::make_unique<Token[]>(1);
31873187
TokenArray[0].startToken();
31883188
TokenArray[0].setKind(tok::annot_pragma_loop_hint);
3189-
TokenArray[0].setLocation(PragmaName.getLocation());
3189+
TokenArray[0].setLocation(Introducer.Loc);
31903190
TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
31913191
TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
31923192
PP.EnterTokenStream(std::move(TokenArray), 1,

clang/lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,8 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
21722172
// Create temporary attribute list.
21732173
ParsedAttributesWithRange TempAttrs(AttrFactory);
21742174

2175+
SourceLocation StartLoc = Tok.getLocation();
2176+
21752177
// Get loop hints and consume annotated token.
21762178
while (Tok.is(tok::annot_pragma_loop_hint)) {
21772179
LoopHint Hint;
@@ -2192,6 +2194,10 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
21922194
Stmts, StmtCtx, TrailingElseLoc, Attrs);
21932195

21942196
Attrs.takeAllFrom(TempAttrs);
2197+
2198+
assert(Attrs.Range.getBegin().isInvalid() &&
2199+
"start of attribute range already set");
2200+
Attrs.Range.setBegin(StartLoc);
21952201
return S;
21962202
}
21972203

clang/test/AST/sourceranges.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,36 @@ namespace attributed_decl {
108108
}
109109
}
110110

111+
// CHECK: NamespaceDecl {{.*}} attributed_stmt
112+
namespace attributed_stmt {
113+
// In DO_PRAGMA and _Pragma cases, `LoopHintAttr` comes from <scratch space>
114+
// file.
115+
116+
#define DO_PRAGMA(x) _Pragma (#x)
117+
118+
void f() {
119+
// CHECK: AttributedStmt {{.*}} <line:[[@LINE-3]]:24, line:[[@LINE+2]]:33>
120+
DO_PRAGMA (unroll(2))
121+
for (int i = 0; i < 10; ++i);
122+
123+
// CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+3]]:33>
124+
// CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:13, col:22>
125+
#pragma unroll(2)
126+
for (int i = 0; i < 10; ++i);
127+
128+
// CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+5]]:33>
129+
// CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:41>
130+
#pragma clang loop vectorize(enable)
131+
// CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:42>
132+
#pragma clang loop interleave(enable)
133+
for (int i = 0; i < 10; ++i);
134+
135+
// CHECK: AttributedStmt {{.*}} <line:[[@LINE+1]]:5, line:[[@LINE+2]]:33>
136+
_Pragma("unroll(2)")
137+
for (int i = 0; i < 10; ++i);
138+
}
139+
}
140+
111141
#if __cplusplus >= 201703L
112142
// CHECK-1Z: FunctionDecl {{.*}} construct_with_init_list
113143
std::map<int, int> construct_with_init_list() {

0 commit comments

Comments
 (0)