https://github.com/isuckatcs updated 
https://github.com/llvm/llvm-project/pull/128265

>From 794e45b99711ebb72be1e4e6263f7f67f5da8545 Mon Sep 17 00:00:00 2001
From: isuckatcs <65320245+isucka...@users.noreply.github.com>
Date: Sat, 1 Mar 2025 17:06:34 +0100
Subject: [PATCH] [clang][diagnostics] add '-Wundef-true' warning option

---
 .../include/clang/Basic/DiagnosticLexKinds.td |  3 +
 clang/lib/Lex/PPExpressions.cpp               |  5 +-
 .../test/Preprocessor/warn-macro-undef-true.c | 78 +++++++++++++++++++
 3 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Preprocessor/warn-macro-undef-true.c

diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td 
b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 4bcef23ccce16..f7c0b2da4a389 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -393,6 +393,9 @@ def pp_macro_not_used : Warning<"macro is not used">, 
DefaultIgnore,
 def warn_pp_undef_identifier : Warning<
   "%0 is not defined, evaluates to 0">,
   InGroup<DiagGroup<"undef">>, DefaultIgnore;
+def warn_pp_undef_true_identifier : Warning<
+  "'true' is not defined, evaluates to 0">,
+  InGroup<DiagGroup<"undef-true">>;
 def warn_pp_undef_prefix : Warning<
   "%0 is not defined, evaluates to 0">,
   InGroup<DiagGroup<"undef-prefix">>, DefaultIgnore;
diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp
index a3b1384f0fa1d..831cb01d7387c 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -260,7 +260,8 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, 
DefinedTracker &DT,
           PP.Diag(PeekTok, diag::warn_pp_undef_identifier) << II;
 
           const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
-          // If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
+          // If 'Wundef' is enabled, do not emit 'undef-prefix' or 'undef-true'
+          // diagnostics.
           if (DiagEngine.isIgnored(diag::warn_pp_undef_identifier,
                                    PeekTok.getLocation())) {
             const std::vector<std::string> UndefPrefixes =
@@ -272,6 +273,8 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, 
DefinedTracker &DT,
                              }))
               PP.Diag(PeekTok, diag::warn_pp_undef_prefix)
                   << AddFlagValue{llvm::join(UndefPrefixes, ",")} << II;
+            else if(II->getName() == "true")
+              PP.Diag(PeekTok, diag::warn_pp_undef_true_identifier);
           }
         }
         Result.Val = 0;
diff --git a/clang/test/Preprocessor/warn-macro-undef-true.c 
b/clang/test/Preprocessor/warn-macro-undef-true.c
new file mode 100644
index 0000000000000..cb4b206378c0e
--- /dev/null
+++ b/clang/test/Preprocessor/warn-macro-undef-true.c
@@ -0,0 +1,78 @@
+// RUN: %clang_cc1 %s -Eonly -std=c89 -verify=undef-true -verify=undef
+// RUN: %clang_cc1 %s -Eonly -std=c99 -verify=undef-true -verify=undef
+// RUN: %clang_cc1 %s -Eonly -std=c11 -verify=undef-true -verify=undef
+// RUN: %clang_cc1 %s -Eonly -std=c17 -verify=undef-true -verify=undef
+// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true -verify=undef
+
+#if __STDC_VERSION__ >= 202311L
+/* undef-no-diagnostics */
+#endif
+
+#define FOO true
+#if FOO /* #1 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#1 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#if true /* #2 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#2 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#if false || true /* #3 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#3 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#define true 1
+
+#define FOO true
+#if FOO
+#endif
+
+#if true
+#endif
+
+#if false || true
+#endif
+
+#undef true
+
+#define FOO true
+#if FOO /* #4 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#4 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#if true /* #5 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#5 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#if false || true /* #6 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#6 {{'true' is not defined, evaluates to 0}} */
+#endif
+
+#define true true
+#if true /* #7 */
+#endif
+#if __STDC_VERSION__ < 202311L
+/* undef-true-warning@#7 {{'true' is not defined, evaluates to 0}} */
+#endif
+#undef true
+
+/* Test that #pragma-enabled 'Wundef' can override 'Wundef-true' */
+#pragma clang diagnostic warning "-Wundef"
+#if true /* #8 */
+#endif
+#pragma clang diagnostic ignored "-Wundef"
+#if __STDC_VERSION__ < 202311L
+/* undef-warning@#8 {{'true' is not defined, evaluates to 0}} */
+#endif

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to