]> granicus.if.org Git - clang/commitdiff
[Syntax] Fix a crash when dumping empty token buffer
authorIlya Biryukov <ibiryukov@google.com>
Wed, 19 Jun 2019 13:56:36 +0000 (13:56 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Wed, 19 Jun 2019 13:56:36 +0000 (13:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@363801 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Tooling/Syntax/Tokens.cpp
unittests/Tooling/Syntax/TokensTest.cpp

index e22623741c49181349ba679c861827369d5c8b26..b4ecb8524839bc30f3ad94bab8d525764b594b6a 100644 (file)
@@ -477,8 +477,7 @@ std::string TokenBuffer::dumpForTests() const {
 
   auto DumpTokens = [this, &PrintToken](llvm::raw_ostream &OS,
                                         llvm::ArrayRef<syntax::Token> Tokens) {
-    if (Tokens.size() == 1) {
-      assert(Tokens[0].kind() == tok::eof);
+    if (Tokens.empty()) {
       OS << "<empty>";
       return;
     }
@@ -495,7 +494,8 @@ std::string TokenBuffer::dumpForTests() const {
 
   OS << "expanded tokens:\n"
      << "  ";
-  DumpTokens(OS, ExpandedTokens);
+  // (!) we do not show '<eof>'.
+  DumpTokens(OS, llvm::makeArrayRef(ExpandedTokens).drop_back());
   OS << "\n";
 
   std::vector<FileID> Keys;
index 34c80fce2a3b7c87448be42b00d2a65dd56f1bf1..a812b844c85aab972606662088fd12d805598684 100644 (file)
@@ -290,6 +290,14 @@ file './input.cpp'
     # pragma GCC visibility push ( public ) # pragma GCC visibility pop
   mappings:
     ['#'_0, '<eof>'_13) => ['<eof>'_0, '<eof>'_0)
+)"},
+      // Empty files should not crash.
+      {R"cpp()cpp", R"(expanded tokens:
+  <empty>
+file './input.cpp'
+  spelled tokens:
+    <empty>
+  no mappings.
 )"}};
   for (auto &Test : TestCases)
     EXPECT_EQ(collectAndDump(Test.first), Test.second)