zixuw updated this revision to Diff 269628.
zixuw added a comment.

Update test case for `#pragma clang diagnostic`
Update test case to verify that the state of `-Wundef` can still be modified 
using `#pragma clang diagnostic`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80751/new/

https://reviews.llvm.org/D80751

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticLexKinds.td
  clang/include/clang/Basic/DiagnosticOptions.h
  clang/include/clang/Driver/Options.td
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/lib/Lex/PPExpressions.cpp
  clang/test/Preprocessor/warn-macro-undef-prefix.c
  clang/test/Preprocessor/warn-macro-undef.c

Index: clang/test/Preprocessor/warn-macro-undef.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/warn-macro-undef.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -Eonly -Wundef -verify
+// RUN: %clang_cc1 %s -Eonly -Wundef -Wundef-prefix=A -verify
+
+extern int x;
+
+#if A // expected-warning {{'A' is not defined, evaluates to 0}}
+#endif
+
+#pragma clang diagnostic ignored "-Wundef"
+
+#if B // no warning
+#endif
+
+#pragma clang diagnostic warning "-Wundef"
+
+#if B // expected-warning {{'B' is not defined, evaluates to 0}}
+#endif
Index: clang/test/Preprocessor/warn-macro-undef-prefix.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/warn-macro-undef-prefix.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 %s -Eonly -Wundef-prefix=A,BC -verify
+
+extern int x;
+
+#if A // expected-warning {{'A' is not defined, evaluates to 0}}
+#endif
+
+#if A1 // expected-warning {{'A1' is not defined, evaluates to 0}}
+#endif
+
+#if B // no warning
+#endif
+
+#define BC 0
+#if BC // no warning
+#endif
+
+#undef BC
+#if BC // expected-warning {{'BC' is not defined, evaluates to 0}}
+#endif
+
+#if BC1 // expected-warning {{'BC1' is not defined, evaluates to 0}}
+#endif
Index: clang/lib/Lex/PPExpressions.cpp
===================================================================
--- clang/lib/Lex/PPExpressions.cpp
+++ clang/lib/Lex/PPExpressions.cpp
@@ -251,8 +251,21 @@
         // If this identifier isn't 'defined' or one of the special
         // preprocessor keywords and it wasn't macro expanded, it turns
         // into a simple 0
-        if (ValueLive)
-          PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
+        if (ValueLive) {
+          const auto &UndefPrefixes =
+              PP.getDiagnostics().getDiagnosticOptions().UndefPrefixes;
+          const auto &IdentifierName = II->getName();
+          // Check whether UndefPrefixes is empty for standalone
+          // "-Werror=undef" to work.
+          // "-Werror=undef" implies "-Wundef" but does not add an empty
+          // string to UndefPrefixes as an explicit "-Wundef" does.
+          if (UndefPrefixes.empty() ||
+              llvm::any_of(UndefPrefixes,
+                           [&IdentifierName](const auto &Prefix) {
+                             return IdentifierName.startswith(Prefix);
+                           }))
+            PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
+        }
         Result.Val = 0;
         Result.Val.setIsUnsigned(false); // "0" is signed intmax_t 0.
         Result.setIdentifier(II);
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -1820,6 +1820,16 @@
   }
   Opts.MessageLength =
       getLastArgIntValue(Args, OPT_fmessage_length_EQ, 0, Diags);
+
+  for (const auto *A : Args.filtered(OPT_Wundef_prefix_EQ)) {
+    SmallVector<StringRef, 2> Prefixes;
+    // Keep empty strings to allow '-Wundef' (aliased to '-Wundef-prefix=""')
+    // to generate diagnostics for all undefined macros.
+    StringRef{A->getValue()}.split(Prefixes, ",");
+    for (const auto &P : Prefixes)
+      Opts.UndefPrefixes.push_back(P.str());
+  }
+
   addDiagnosticArgs(Args, OPT_W_Group, OPT_W_value_Group, Opts.Warnings);
   addDiagnosticArgs(Args, OPT_R_Group, OPT_R_value_Group, Opts.Remarks);
 
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -465,6 +465,14 @@
 def Wp_COMMA : CommaJoined<["-"], "Wp,">,
   HelpText<"Pass the comma separated arguments in <arg> to the preprocessor">,
   MetaVarName<"<arg>">, Group<Preprocessor_Group>;
+def Wundef_prefix_EQ : Joined<["-"], "Wundef-prefix=">, Group<W_value_Group>,
+  Flags<[CC1Option, CoreOption]>, MetaVarName<"<arg>">,
+  HelpText<"Enable warnings for undefined macros with a prefix in the comma separated list <arg>">;
+// Rely on the implementation that a Flag alias for a Joined option is provided a default argument
+// of an empty string (""). So we have -Wundef => -Wundef-prefix=""
+def Wundef: Flag<["-"], "Wundef">, Group<W_Group>, Flags<[CC1Option, CoreOption]>,
+  Alias<Wundef_prefix_EQ>,
+  HelpText<"Aliased to '-Wundef-prefix=\"\"', enable warnings for undefined macros">;
 def Wwrite_strings : Flag<["-"], "Wwrite-strings">, Group<W_Group>, Flags<[CC1Option, HelpHidden]>;
 def Wno_write_strings : Flag<["-"], "Wno-write-strings">, Group<W_Group>, Flags<[CC1Option, HelpHidden]>;
 def W_Joined : Joined<["-"], "W">, Group<W_Group>, Flags<[CC1Option, CoreOption]>,
Index: clang/include/clang/Basic/DiagnosticOptions.h
===================================================================
--- clang/include/clang/Basic/DiagnosticOptions.h
+++ clang/include/clang/Basic/DiagnosticOptions.h
@@ -98,6 +98,10 @@
   /// prefixes removed.
   std::vector<std::string> Warnings;
 
+  /// The list of prefixes from -Wundef-prefix=... used to generate warnings
+  /// for undefined macros.
+  std::vector<std::string> UndefPrefixes;
+
   /// The list of -R... options used to alter the diagnostic mappings, with the
   /// prefixes removed.
   std::vector<std::string> Remarks;
Index: clang/include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticLexKinds.td
+++ clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -311,7 +311,7 @@
   InGroup<DiagGroup<"unused-macros">>;
 def warn_pp_undef_identifier : Warning<
   "%0 is not defined, evaluates to 0">,
-  InGroup<DiagGroup<"undef">>, DefaultIgnore;
+  InGroup<Undefined>, DefaultIgnore;
 def warn_pp_ambiguous_macro : Warning<
   "ambiguous expansion of macro %0">, InGroup<AmbiguousMacro>;
 def note_pp_ambiguous_macro_chosen : Note<
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -104,6 +104,8 @@
 def FrameAddress : DiagGroup<"frame-address">;
 def DoublePromotion : DiagGroup<"double-promotion">;
 def EnumTooLarge : DiagGroup<"enum-too-large">;
+def Undefined : DiagGroup<"undef">;
+def UndefinedPrefix : DiagGroup<"undef-prefix", [Undefined]>;
 def UnsupportedNan : DiagGroup<"unsupported-nan">;
 def UnsupportedAbs : DiagGroup<"unsupported-abs">;
 def UnsupportedCB : DiagGroup<"unsupported-cb">;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to