Add begin source location for the attributed statement created from PragmaLoopHint...
authorYuanfang Chen <yuanfang.chen@sony.com>
Mon, 1 Jun 2020 02:51:15 +0000 (19:51 -0700)
committerYuanfang Chen <yuanfang.chen@sony.com>
Tue, 9 Jun 2020 17:08:40 +0000 (10:08 -0700)
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

clang/lib/Parse/ParsePragma.cpp
clang/lib/Parse/ParseStmt.cpp
clang/test/AST/sourceranges.cpp

index 816aaf9..2631c35 100644 (file)
@@ -3099,7 +3099,7 @@ void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
     Token LoopHintTok;
     LoopHintTok.startToken();
     LoopHintTok.setKind(tok::annot_pragma_loop_hint);
-    LoopHintTok.setLocation(PragmaName.getLocation());
+    LoopHintTok.setLocation(Introducer.Loc);
     LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
     LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
     TokenList.push_back(LoopHintTok);
@@ -3186,7 +3186,7 @@ void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
   auto TokenArray = std::make_unique<Token[]>(1);
   TokenArray[0].startToken();
   TokenArray[0].setKind(tok::annot_pragma_loop_hint);
-  TokenArray[0].setLocation(PragmaName.getLocation());
+  TokenArray[0].setLocation(Introducer.Loc);
   TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
   TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
   PP.EnterTokenStream(std::move(TokenArray), 1,
index 84166bb..d00f6b6 100644 (file)
@@ -2172,6 +2172,8 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
   // Create temporary attribute list.
   ParsedAttributesWithRange TempAttrs(AttrFactory);
 
+  SourceLocation StartLoc = Tok.getLocation();
+
   // Get loop hints and consume annotated token.
   while (Tok.is(tok::annot_pragma_loop_hint)) {
     LoopHint Hint;
@@ -2192,6 +2194,10 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
       Stmts, StmtCtx, TrailingElseLoc, Attrs);
 
   Attrs.takeAllFrom(TempAttrs);
+
+  assert(Attrs.Range.getBegin().isInvalid() &&
+         "start of attribute range already set");
+  Attrs.Range.setBegin(StartLoc);
   return S;
 }
 
index 3c023c8..a38ec32 100644 (file)
@@ -108,6 +108,36 @@ namespace attributed_decl {
   }\r
 }\r
 \r
+// CHECK: NamespaceDecl {{.*}} attributed_stmt\r
+namespace attributed_stmt {\r
+  // In DO_PRAGMA and _Pragma cases, `LoopHintAttr` comes from <scratch space>\r
+  // file.\r
+\r
+  #define DO_PRAGMA(x) _Pragma (#x)\r
+\r
+  void f() {\r
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE-3]]:24, line:[[@LINE+2]]:33>\r
+    DO_PRAGMA (unroll(2))\r
+    for (int i = 0; i < 10; ++i);\r
+\r
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+3]]:33>\r
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:13, col:22>\r
+    #pragma unroll(2)\r
+    for (int i = 0; i < 10; ++i);\r
+\r
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+5]]:33>\r
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:41>\r
+    #pragma clang loop vectorize(enable)\r
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:42>\r
+    #pragma clang loop interleave(enable)\r
+    for (int i = 0; i < 10; ++i);\r
+\r
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+1]]:5, line:[[@LINE+2]]:33>\r
+    _Pragma("unroll(2)")\r
+    for (int i = 0; i < 10; ++i);\r
+  }\r
+}\r
+\r
 #if __cplusplus >= 201703L\r
 // CHECK-1Z: FunctionDecl {{.*}} construct_with_init_list\r
 std::map<int, int> construct_with_init_list() {\r