]> granicus.if.org Git - clang/commitdiff
add support for -Wundef.
authorChris Lattner <sabre@nondot.org>
Wed, 23 Jan 2008 17:19:46 +0000 (17:19 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 23 Jan 2008 17:19:46 +0000 (17:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46274 91177308-0d34-0410-b5e6-96231b3b80d8

Driver/clang.cpp
Lex/PPExpressions.cpp
include/clang/Basic/DiagnosticKinds.def
test/Preprocessor/if_warning.c [new file with mode: 0644]

index 295ddd7d52cc50d1f33dcc550ad324dd39eb6615..02f8321b689335f88a20b02da9371ba0b8e2005d 100644 (file)
@@ -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);
 }
 
 //===----------------------------------------------------------------------===//
index 22c03af9e093edd96c47cd8b5b0a0fcbd610fd5e..bd7dda4d9a165f63db1c3200e969e80ec6a8c777 100644 (file)
@@ -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);
index 81cffac0c7cd5f3e4f14eaaee3068158ecc97668..0fcd8c93b5b03321f849f4d73aa738b4a46a5a9e 100644 (file)
@@ -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 (file)
index 0000000..bf30d30
--- /dev/null
@@ -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
+