[PATCH] D75461: Ignore macro expansions when scanning for fallthrough comments

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 2 08:12:05 PST 2020


tbaeder created this revision.
tbaeder added a reviewer: tstellar.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

When scanning for fallthrough comments, it's not useful to go to the definition of a macro. We instead only want to search at the expanded source code location. Includes a test that failed before.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75461

Files:
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/Sema/fallthrough-comment.c


Index: clang/test/Sema/fallthrough-comment.c
===================================================================
--- clang/test/Sema/fallthrough-comment.c
+++ clang/test/Sema/fallthrough-comment.c
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -std=c11 -verify -Wimplicit-fallthrough %s
 
+#define SOME_CONSTANT 1
+
 int fallthrough_comment(int n) {
   switch (n) {
   case 0:
@@ -10,9 +12,13 @@
 
     /*fall-through.*/
 
-  case 2:
+  case 3:
+    n += SOME_CONSTANT;
+    /* FALL THRU */
+
+  case 4:
     n++;
-  case 3: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '__attribute__((fallthrough));' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
+  case 5: // expected-warning{{unannotated fall-through between switch labels}} expected-note{{insert '__attribute__((fallthrough));' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
     n++;
     break;
   }
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===================================================================
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -1216,9 +1216,10 @@
     bool isFollowedByFallThroughComment(const Stmt *Statement) {
       // Try to detect whether the fallthough is marked by a comment like
       // /*FALLTHOUGH*/.
+      auto Loc = S.getSourceManager().getExpansionLoc(Statement->getEndLoc());
       bool Invalid;
-      const char *SourceData = S.getSourceManager().getCharacterData(
-          Statement->getEndLoc(), &Invalid);
+      const char *SourceData =
+          S.getSourceManager().getCharacterData(Loc, &Invalid);
       if (Invalid)
         return false;
       const char *LineStart = SourceData;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75461.247649.patch
Type: text/x-patch
Size: 1773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200302/b611eee0/attachment-0001.bin>


More information about the cfe-commits mailing list