[clang] 9c2e770 - Add begin source location for the attributed statement created from PragmaLoopHint decorated loop

Yuanfang Chen via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 9 10:09:17 PDT 2020


Author: Yuanfang Chen
Date: 2020-06-09T10:08:40-07:00
New Revision: 9c2e770034d04a0966b84cac14a9e3fb3b0d4ab7

URL: https://github.com/llvm/llvm-project/commit/9c2e770034d04a0966b84cac14a9e3fb3b0d4ab7
DIFF: https://github.com/llvm/llvm-project/commit/9c2e770034d04a0966b84cac14a9e3fb3b0d4ab7.diff

LOG: 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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index 816aaf9f0956..2631c3556273 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -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,

diff  --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 84166bbbdc7b..d00f6b640fb4 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -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;
 }
 

diff  --git a/clang/test/AST/sourceranges.cpp b/clang/test/AST/sourceranges.cpp
index 3c023c868946..a38ec3207ae4 100644
--- a/clang/test/AST/sourceranges.cpp
+++ b/clang/test/AST/sourceranges.cpp
@@ -108,6 +108,36 @@ namespace attributed_decl {
   }
 }
 
+// CHECK: NamespaceDecl {{.*}} attributed_stmt
+namespace attributed_stmt {
+  // In DO_PRAGMA and _Pragma cases, `LoopHintAttr` comes from <scratch space>
+  // file.
+
+  #define DO_PRAGMA(x) _Pragma (#x)
+
+  void f() {
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE-3]]:24, line:[[@LINE+2]]:33>
+    DO_PRAGMA (unroll(2))
+    for (int i = 0; i < 10; ++i);
+
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+3]]:33>
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:13, col:22>
+    #pragma unroll(2)
+    for (int i = 0; i < 10; ++i);
+
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+2]]:5, line:[[@LINE+5]]:33>
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:41>
+    #pragma clang loop vectorize(enable)
+    // CHECK: LoopHintAttr {{.*}} <line:[[@LINE+1]]:19, col:42>
+    #pragma clang loop interleave(enable)
+    for (int i = 0; i < 10; ++i);
+
+    // CHECK: AttributedStmt {{.*}} <line:[[@LINE+1]]:5, line:[[@LINE+2]]:33>
+    _Pragma("unroll(2)")
+    for (int i = 0; i < 10; ++i);
+  }
+}
+
 #if __cplusplus >= 201703L
 // CHECK-1Z: FunctionDecl {{.*}} construct_with_init_list
 std::map<int, int> construct_with_init_list() {


        


More information about the cfe-commits mailing list