[clang] 3a11d5a - [clang][diagnostics] add `-Wundef-true` warning option (#128265)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 2 10:00:31 PST 2025
Author: isuckatcs
Date: 2025-03-02T19:00:27+01:00
New Revision: 3a11d5a8dfb6c95a5ba0c6b4463e15494005a369
URL: https://github.com/llvm/llvm-project/commit/3a11d5a8dfb6c95a5ba0c6b4463e15494005a369
DIFF: https://github.com/llvm/llvm-project/commit/3a11d5a8dfb6c95a5ba0c6b4463e15494005a369.diff
LOG: [clang][diagnostics] add `-Wundef-true` warning option (#128265)
New option `-Wundef-true` added and enabled by default to warn when `true` is used in the C preprocessor without being defined before C23.
Added:
clang/test/Preprocessor/warn-macro-undef-true.c
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticLexKinds.td
clang/lib/Lex/PPExpressions.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c97bbc372fcb7..d2fc1ce07dd79 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -114,6 +114,8 @@ Non-comprehensive list of changes in this release
New Compiler Flags
------------------
+- New option ``-Wundef-true`` added and enabled by default to warn when `true` is used in the C preprocessor without being defined before C23.
+
- New option ``-fprofile-continuous`` added to enable continuous profile syncing to file (#GH124353, `docs <https://clang.llvm.org/docs/UsersManual.html#cmdoption-fprofile-continuous>`_).
The feature has `existed <https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program>`_)
for a while and this is just a user facing option.
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 77520447b813d..fac80fb4009aa 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -791,6 +791,8 @@ def ReservedIdAsMacroAlias : DiagGroup<"reserved-id-macro", [ReservedIdAsMacro]>
def ReservedAttributeIdentifier : DiagGroup<"reserved-attribute-identifier">;
def RestrictExpansionMacro : DiagGroup<"restrict-expansion">;
def FinalMacro : DiagGroup<"final-macro">;
+def UndefinedTrueIdentifier : DiagGroup<"undef-true">;
+def UndefinedIdentifier : DiagGroup<"undef", [UndefinedTrueIdentifier]>;
// Just silence warnings about -Wstrict-aliasing for now.
def : DiagGroup<"strict-aliasing=0">;
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td
index 2b1cc81677b08..8daf3602450f0 100644
--- a/clang/include/clang/Basic/DiagnosticLexKinds.td
+++ b/clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -392,7 +392,10 @@ def pp_macro_not_used : Warning<"macro is not used">, DefaultIgnore,
InGroup<DiagGroup<"unused-macros">>;
def warn_pp_undef_identifier : Warning<
"%0 is not defined, evaluates to 0">,
- InGroup<DiagGroup<"undef">>, DefaultIgnore;
+ InGroup<UndefinedIdentifier>, DefaultIgnore;
+def warn_pp_undef_true_identifier : Warning<
+ "'true' is not defined, evaluates to 0">,
+ InGroup<UndefinedTrueIdentifier>;
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..b031571907441 100644
--- a/clang/lib/Lex/PPExpressions.cpp
+++ b/clang/lib/Lex/PPExpressions.cpp
@@ -257,12 +257,14 @@ static bool EvaluateValue(PPValue &Result, Token &PeekTok, DefinedTracker &DT,
// 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;
+ unsigned DiagID = II->getName() == "true"
+ ? diag::warn_pp_undef_true_identifier
+ : diag::warn_pp_undef_identifier;
+ PP.Diag(PeekTok, DiagID) << II;
const DiagnosticsEngine &DiagEngine = PP.getDiagnostics();
// If 'Wundef' is enabled, do not emit 'undef-prefix' diagnostics.
- if (DiagEngine.isIgnored(diag::warn_pp_undef_identifier,
- PeekTok.getLocation())) {
+ if (DiagEngine.isIgnored(DiagID, PeekTok.getLocation())) {
const std::vector<std::string> UndefPrefixes =
DiagEngine.getDiagnosticOptions().UndefPrefixes;
const StringRef IdentifierName = II->getName();
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..9a64d577a96ed
--- /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
+// RUN: %clang_cc1 %s -Eonly -std=c99 -verify=undef-true
+// RUN: %clang_cc1 %s -Eonly -std=c11 -verify=undef-true
+// RUN: %clang_cc1 %s -Eonly -std=c17 -verify=undef-true
+// RUN: %clang_cc1 %s -Eonly -std=c23 -verify=undef-true
+
+#if __STDC_VERSION__ >= 202311L
+/* undef-true-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-true-warning@#8 {{'true' is not defined, evaluates to 0}} */
+#endif
More information about the cfe-commits
mailing list