]> granicus.if.org Git - clang/commitdiff
If we see an invalid #ifdef directive, enter a conditional compilation region
authorChris Lattner <sabre@nondot.org>
Mon, 24 Sep 2007 05:14:57 +0000 (05:14 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 24 Sep 2007 05:14:57 +0000 (05:14 +0000)
so that we don't emit an error on the #endif.  Suggestion by Neil.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42258 91177308-0d34-0410-b5e6-96231b3b80d8

Lex/Preprocessor.cpp
test/Preprocessor/ifdef-recover.c [new file with mode: 0644]

index b51222cefd3bdcb9c1dbe822f2f7463d3b9221d6..f43eae1c9823e15d88f42ae0e075e258ab446cd8 100644 (file)
@@ -2121,8 +2121,13 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
   ReadMacroName(MacroNameTok);
   
   // Error reading macro name?  If so, diagnostic already issued.
-  if (MacroNameTok.getKind() == tok::eom)
+  if (MacroNameTok.getKind() == tok::eom) {
+    // Skip code until we get to #endif.  This helps with recovery by not
+    // emitting an error when the #endif is reached.
+    SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
+                                 /*Foundnonskip*/false, /*FoundElse*/false);
     return;
+  }
   
   // Check to see if this is the last token on the #if[n]def line.
   CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
diff --git a/test/Preprocessor/ifdef-recover.c b/test/Preprocessor/ifdef-recover.c
new file mode 100644 (file)
index 0000000..2009514
--- /dev/null
@@ -0,0 +1,7 @@
+/* RUN: clang %s 2>&1 | grep error: | count 1
+ */
+
+#ifdef
+
+#endif
+