Fix -Wcompound-token-split to give the same warnings under -E
authorRichard Smith <richard@metafoo.co.uk>
Tue, 1 Sep 2020 02:04:35 +0000 (19:04 -0700)
committerRichard Smith <richard@metafoo.co.uk>
Tue, 1 Sep 2020 03:59:20 +0000 (20:59 -0700)
-frewrite-includes.

Remove the special-case (and highly implausible) diagnostic for a
compound token that crosses a file boundary, and instead model that case
the same as a compound token separated by whitespace, so that file
transitions and presumed file transitions behave the same way.

clang/include/clang/Basic/DiagnosticParseKinds.td
clang/lib/Parse/Parser.cpp
clang/test/Parser/compound-token-split.cpp

index 42da8bb..0e51fef 100644 (file)
@@ -70,9 +70,6 @@ def subst_compound_token_kind : TextSubstitution<
 def warn_compound_token_split_by_macro : Warning<
   "%sub{subst_compound_token_kind}0,1,2,3 appear in different "
   "macro expansion contexts">, InGroup<CompoundTokenSplitByMacro>;
-def warn_compound_token_split_by_file : Warning<
-  "%sub{subst_compound_token_kind}0,1,2,3 appear in different source files">,
-  InGroup<CompoundTokenSplit>;
 def note_compound_token_split_second_token_here : Note<
   "%select{|second }0%1 token is here">;
 def warn_compound_token_split_by_whitespace : Warning<
index 70e6e74..c72ffde 100644 (file)
@@ -233,13 +233,12 @@ void Parser::checkCompoundToken(SourceLocation FirstTokLoc,
     return;
   SourceLocation SecondTokLoc = Tok.getLocation();
 
-  // We expect both tokens to come from the same FileID.
-  FileID FirstID = PP.getSourceManager().getFileID(FirstTokLoc);
-  FileID SecondID = PP.getSourceManager().getFileID(SecondTokLoc);
-  if (FirstID != SecondID) {
-    Diag(FirstTokLoc, (FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID())
-                          ? diag::warn_compound_token_split_by_macro
-                          : diag::warn_compound_token_split_by_file)
+  // If either token is in a macro, we expect both tokens to come from the same
+  // macro expansion.
+  if ((FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID()) &&
+      PP.getSourceManager().getFileID(FirstTokLoc) !=
+          PP.getSourceManager().getFileID(SecondTokLoc)) {
+    Diag(FirstTokLoc, diag::warn_compound_token_split_by_macro)
         << (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
         << static_cast<int>(Op) << SourceRange(FirstTokLoc);
     Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here)
@@ -249,7 +248,7 @@ void Parser::checkCompoundToken(SourceLocation FirstTokLoc,
   }
 
   // We expect the tokens to abut.
-  if (Tok.hasLeadingSpace()) {
+  if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
     SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc);
     if (SpaceLoc.isInvalid())
       SpaceLoc = FirstTokLoc;
index 0f1774a..6b77bf3 100644 (file)
@@ -1,8 +1,12 @@
 // RUN: %clang_cc1 %s -verify
 // RUN: %clang_cc1 %s -verify=expected,space -Wcompound-token-split
 
+// Ensure we get the same warnings after -frewrite-includes
+// RUN: %clang_cc1 %s -E -frewrite-includes -o %t
+// RUN: %clang_cc1 -x c++ %t -verify=expected,space -Wcompound-token-split
+
 #ifdef LSQUARE
-[ // expected-note {{second '[' token is here}}
+[
 #else
 
 #define VAR(type, name, init) type name = (init)
@@ -26,7 +30,7 @@ int f2() {
   return n;
 }
 
-[ // expected-warning-re {{{{^}}'[' tokens introducing attribute appear in different source files}}
+[ // space-warning-re {{{{^}}'[' tokens introducing attribute are separated by whitespace}}
 #define LSQUARE
 #include __FILE__
   noreturn ]]  void g();