aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Driver/clang.cpp8
-rw-r--r--Lex/PPExpressions.cpp1
-rw-r--r--include/clang/Basic/DiagnosticKinds.def2
-rw-r--r--test/Preprocessor/if_warning.c12
4 files changed, 22 insertions, 1 deletions
diff --git a/Driver/clang.cpp b/Driver/clang.cpp
index 295ddd7d52..02f8321b68 100644
--- a/Driver/clang.cpp
+++ b/Driver/clang.cpp
@@ -404,6 +404,11 @@ static llvm::cl::opt<bool>
WarnNoFormatNonLiteral("Wno-format-nonliteral",
llvm::cl::desc("Do not warn about non-literal format strings."));
+static llvm::cl::opt<bool>
+WarnUndefMacros("Wundef",
+ llvm::cl::desc("Warn on use of undefined macros in #if's"));
+
+
/// InitializeDiagnostics - Initialize the diagnostic object, based on the
/// current command line option settings.
static void InitializeDiagnostics(Diagnostic &Diags) {
@@ -423,7 +428,8 @@ static void InitializeDiagnostics(Diagnostic &Diags) {
if (WarnNoFormatNonLiteral)
Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant,
diag::MAP_IGNORE);
-
+ if (!WarnUndefMacros)
+ Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE);
}
//===----------------------------------------------------------------------===//
diff --git a/Lex/PPExpressions.cpp b/Lex/PPExpressions.cpp
index 22c03af9e0..bd7dda4d9a 100644
--- a/Lex/PPExpressions.cpp
+++ b/Lex/PPExpressions.cpp
@@ -74,6 +74,7 @@ static bool EvaluateValue(llvm::APSInt &Result, Token &PeekTok,
// into a simple 0, unless it is the C++ keyword "true", in which case it
// turns into "1".
if (II->getPPKeywordID() != tok::pp_defined) {
+ PP.Diag(PeekTok, diag::warn_pp_undef_identifier, II->getName());
Result = II->getTokenID() == tok::kw_true;
Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
PP.LexNonComment(PeekTok);
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index 81cffac0c7..0fcd8c93b5 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -206,6 +206,8 @@ DIAG(err_pp_expected_rparen, ERROR,
"expected ')' in preprocessor expression")
DIAG(err_pp_expected_eol, ERROR,
"expected end of line in preprocessor expression")
+DIAG(warn_pp_undef_identifier, WARNING,
+ "\"%0\" is not defined, evaluates to 0")
DIAG(err_pp_defined_requires_identifier, ERROR,
"operator \"defined\" requires an identifier")
DIAG(err_pp_missing_rparen, ERROR,
diff --git a/test/Preprocessor/if_warning.c b/test/Preprocessor/if_warning.c
new file mode 100644
index 0000000000..bf30d303b6
--- /dev/null
+++ b/test/Preprocessor/if_warning.c
@@ -0,0 +1,12 @@
+// RUN: clang %s -E -Wundef -Werror 2>&1 | grep error | count 1 &&
+// RUN: clang %s -E -Werror 2>&1 | not grep error
+
+#if foo // Should generate an warning
+#endif
+
+#ifdef foo
+#endif
+
+#if defined(foo)
+#endif
+