r363801 - [Syntax] Fix a crash when dumping empty token buffer

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 19 06:56:36 PDT 2019


Author: ibiryukov
Date: Wed Jun 19 06:56:36 2019
New Revision: 363801

URL: http://llvm.org/viewvc/llvm-project?rev=363801&view=rev
Log:
[Syntax] Fix a crash when dumping empty token buffer

Modified:
    cfe/trunk/lib/Tooling/Syntax/Tokens.cpp
    cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp

Modified: cfe/trunk/lib/Tooling/Syntax/Tokens.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Syntax/Tokens.cpp?rev=363801&r1=363800&r2=363801&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Syntax/Tokens.cpp (original)
+++ cfe/trunk/lib/Tooling/Syntax/Tokens.cpp Wed Jun 19 06:56:36 2019
@@ -477,8 +477,7 @@ std::string TokenBuffer::dumpForTests()
 
   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()
 
   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;

Modified: cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp?rev=363801&r1=363800&r2=363801&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/Syntax/TokensTest.cpp Wed Jun 19 06:56:36 2019
@@ -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)




More information about the cfe-commits mailing list