]> granicus.if.org Git - clang/commitdiff
Fix raw lex crash and -frewrite-includes noeol-at-eof failure
authorAlp Toker <alp@nuanti.com>
Fri, 13 Dec 2013 17:04:55 +0000 (17:04 +0000)
committerAlp Toker <alp@nuanti.com>
Fri, 13 Dec 2013 17:04:55 +0000 (17:04 +0000)
Raw lexers don't have a preprocessor so we need to null check.

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

lib/Lex/Lexer.cpp
lib/Rewrite/Frontend/InclusionRewriter.cpp
test/Frontend/rewrite-includes-eof.c [new file with mode: 0644]

index c071455da6623173b2d9b4b96946ef58658dad51..009f18ad234974b6b43c85fbec8e2d2c7a65ac86 100644 (file)
@@ -2460,7 +2460,8 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
     FormTokenWithChars(Result, CurPtr, tok::eod);
 
     // Restore comment saving mode, in case it was disabled for directive.
-    resetExtendedTokenMode();
+    if (PP)
+      resetExtendedTokenMode();
     return true;  // Have a token.
   }
  
index a2e0db6961982856c8a1f2cfc88122d79491ae7b..0e3e53498b53c6e01a16599066c4a053f3d10aff 100644 (file)
@@ -77,7 +77,7 @@ private:
   void OutputContentUpTo(const MemoryBuffer &FromFile,
                          unsigned &WriteFrom, unsigned WriteTo,
                          StringRef EOL, int &lines,
-                         bool EnsureNewline = false);
+                         bool EnsureNewline);
   void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
                            const MemoryBuffer &FromFile, StringRef EOL,
                            unsigned &NextToWrite, int &Lines);
@@ -250,7 +250,7 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
                                             StringRef EOL,
                                             unsigned &NextToWrite, int &Line) {
   OutputContentUpTo(FromFile, NextToWrite,
-    SM.getFileOffset(StartToken.getLocation()), EOL, Line);
+    SM.getFileOffset(StartToken.getLocation()), EOL, Line, false);
   Token DirectiveToken;
   do {
     DirectiveLex.LexFromRawLexer(DirectiveToken);
@@ -258,7 +258,7 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
   OS << "#if 0 /* expanded by -frewrite-includes */" << EOL;
   OutputContentUpTo(FromFile, NextToWrite,
     SM.getFileOffset(DirectiveToken.getLocation()) + DirectiveToken.getLength(),
-    EOL, Line);
+    EOL, Line, true);
   OS << "#endif /* expanded by -frewrite-includes */" << EOL;
 }
 
@@ -462,12 +462,12 @@ bool InclusionRewriter::Process(FileID FileId,
                 // Replace the macro with (0) or (1), followed by the commented
                 // out macro for reference.
                 OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc),
-                                  EOL, Line);
+                                  EOL, Line, false);
                 OS << '(' << (int) HasFile << ")/*";
                 OutputContentUpTo(FromFile, NextToWrite,
                                   SM.getFileOffset(RawToken.getLocation()) +
                                   RawToken.getLength(),
-                                  EOL, Line);
+                                  EOL, Line, false);
                 OS << "*/";
               }
             } while (RawToken.isNot(tok::eod));
diff --git a/test/Frontend/rewrite-includes-eof.c b/test/Frontend/rewrite-includes-eof.c
new file mode 100644 (file)
index 0000000..af7fd89
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -E -frewrite-includes -I %S/Inputs %s
+// expected-no-diagnostics
+// Note: there's no newline at the end of this C file.
+#include "rewrite-includes-bom.h"
\ No newline at end of file