From 089ee1554c74f12bba8dddcf14cb62feeab6a08b Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sun, 16 Jun 2013 05:05:39 +0000 Subject: [PATCH] PR16339: Don't produce a diagnostic pointing at the whitespace between a '#if' and a '!defined(X)' if we find a broken header guard. This is suboptimal; we should point the diagnostic at the 'X' token not the 'if' token, but it fixes the crash. llvm-svn: 184054 --- clang/lib/Lex/PPDirectives.cpp | 3 ++- clang/test/Lexer/Inputs/bad-header-guard-defined.h | 4 ++++ clang/test/Lexer/header.cpp | 12 +++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 clang/test/Lexer/Inputs/bad-header-guard-defined.h diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 0cfdac92d23a..3e3312a9261a 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2132,7 +2132,8 @@ void Preprocessor::HandleIfDirective(Token &IfToken, // directive seen, handle it for the multiple-include optimization. if (CurPPLexer->getConditionalStackDepth() == 0) { if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) - CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, ConditionalBegin); + // FIXME: Pass in the location of the macro name, not the 'if' token. + CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation()); else CurPPLexer->MIOpt.EnterTopLevelConditional(); } diff --git a/clang/test/Lexer/Inputs/bad-header-guard-defined.h b/clang/test/Lexer/Inputs/bad-header-guard-defined.h new file mode 100644 index 000000000000..b28f1a8873e9 --- /dev/null +++ b/clang/test/Lexer/Inputs/bad-header-guard-defined.h @@ -0,0 +1,4 @@ +#if !defined(foo) +#define goo +int n; +#endif diff --git a/clang/test/Lexer/header.cpp b/clang/test/Lexer/header.cpp index 278ff2e43237..047240ac0f7a 100644 --- a/clang/test/Lexer/header.cpp +++ b/clang/test/Lexer/header.cpp @@ -17,6 +17,16 @@ // CHECK: {{^}} ^~~~~~~~~ // CHECK: {{^}} bad_header_guard +#include "Inputs/bad-header-guard-defined.h" +// CHECK: In file included from {{.*}}header.cpp:{{[0-9]*}}: +// CHECK: {{.*}}bad-header-guard-defined.h:1:2: warning: 'foo' is used as a header guard here, followed by #define of a different macro +// CHECK: {{^}}#if !defined(foo) +// CHECK: {{^}} ^~ +// CHECK: {{.*}}bad-header-guard-defined.h:2:9: note: 'goo' is defined here; did you mean 'foo'? +// CHECK: {{^}}#define goo +// CHECK: {{^}} ^~~ +// CHECK: {{^}} foo + #include "Inputs/multiple.h" #include "Inputs/multiple.h" #include "Inputs/multiple.h" @@ -30,4 +40,4 @@ // CHECK: {{^}} ^~~~~ // CHECK: {{^}} multiple -// CHECK: 2 warnings generated. +// CHECK: 3 warnings generated.