From: Chris Lattner Date: Wed, 23 Jan 2008 17:19:46 +0000 (+0000) Subject: add support for -Wundef. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=116a4b148ec9ad3f3a60044c7fb03f28dc620b7b;p=clang add support for -Wundef. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46274 91177308-0d34-0410-b5e6-96231b3b80d8 --- 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 WarnNoFormatNonLiteral("Wno-format-nonliteral", llvm::cl::desc("Do not warn about non-literal format strings.")); +static llvm::cl::opt +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 +