[clang] 75a1790 - Fix use-after-scope introduced in 850325348ae82cd5e26ea9edfd04219d0fbe7828

Alex Richardson via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 27 07:27:05 PDT 2020


Author: Alex Richardson
Date: 2020-10-27T14:26:23Z
New Revision: 75a1790f4bf052b0a615ed0270a7b4b1c4c88818

URL: https://github.com/llvm/llvm-project/commit/75a1790f4bf052b0a615ed0270a7b4b1c4c88818
DIFF: https://github.com/llvm/llvm-project/commit/75a1790f4bf052b0a615ed0270a7b4b1c4c88818.diff

LOG: Fix use-after-scope introduced in 850325348ae82cd5e26ea9edfd04219d0fbe7828

Added: 
    

Modified: 
    clang/unittests/Format/MacroExpanderTest.cpp
    clang/unittests/Format/TestLexer.h
    clang/unittests/Format/TokenAnnotatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Format/MacroExpanderTest.cpp b/clang/unittests/Format/MacroExpanderTest.cpp
index 20e1dba0d49a0..b2cd5112b820f 100644
--- a/clang/unittests/Format/MacroExpanderTest.cpp
+++ b/clang/unittests/Format/MacroExpanderTest.cpp
@@ -11,6 +11,7 @@ namespace {
 
 class MacroExpanderTest : public ::testing::Test {
 public:
+  MacroExpanderTest() : Lex(Allocator, Buffers) {}
   std::unique_ptr<MacroExpander>
   create(const std::vector<std::string> &MacroDefinitions) {
     return std::make_unique<MacroExpander>(MacroDefinitions,
@@ -64,7 +65,9 @@ class MacroExpanderTest : public ::testing::Test {
           << Context << " in " << text(Tokens) << " at " << File << ":" << Line;
     }
   }
-
+protected:
+  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
+  std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
   TestLexer Lex;
 };
 

diff  --git a/clang/unittests/Format/TestLexer.h b/clang/unittests/Format/TestLexer.h
index 2f2b754fcbbb7..0945e9e66c6fd 100644
--- a/clang/unittests/Format/TestLexer.h
+++ b/clang/unittests/Format/TestLexer.h
@@ -59,12 +59,15 @@ inline std::string text(llvm::ArrayRef<FormatToken *> Tokens) {
 
 class TestLexer : public UnwrappedLineConsumer {
 public:
-  TestLexer(FormatStyle Style = getLLVMStyle())
-      : Style(Style), SourceMgr("test.cpp", ""),
-        IdentTable(getFormattingLangOpts(Style)) {}
+  TestLexer(llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
+            std::vector<std::unique_ptr<llvm::MemoryBuffer>> &Buffers,
+            FormatStyle Style = getLLVMStyle())
+      : Allocator(Allocator), Buffers(Buffers), Style(Style),
+        SourceMgr("test.cpp", ""), IdentTable(getFormattingLangOpts(Style)) {}
 
   TokenList lex(llvm::StringRef Code) {
-    auto Result = getNewLexer(Code).lex();
+    FormatTokenLexer Lex = getNewLexer(Code);
+    ArrayRef<FormatToken *> Result = Lex.lex();
     return TokenList(Result.begin(), Result.end());
   }
 
@@ -77,6 +80,7 @@ class TestLexer : public UnwrappedLineConsumer {
     for (auto &Line : UnwrappedLines) {
       AnnotatedLine Annotated(Line);
       Annotator.annotate(Annotated);
+      Annotator.calculateFormattingInformation(Annotated);
     }
     UnwrappedLines.clear();
     return TokenList(Tokens.begin(), Tokens.end());
@@ -99,18 +103,16 @@ class TestLexer : public UnwrappedLineConsumer {
         llvm::MemoryBuffer::getMemBufferCopy(Code, "<scratch space>"));
     clang::FileID FID =
         SourceMgr.get().createFileID(Buffers.back()->getMemBufferRef());
-    FormatTokenLexer Lex(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
-                         IdentTable);
     return FormatTokenLexer(SourceMgr.get(), FID, 0, Style, Encoding, Allocator,
                             IdentTable);
   }
 
 public:
+  llvm::SpecificBumpPtrAllocator<FormatToken>& Allocator;
+  std::vector<std::unique_ptr<llvm::MemoryBuffer>>& Buffers;
   FormatStyle Style;
   encoding::Encoding Encoding = encoding::Encoding_UTF8;
-  std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
   clang::SourceManagerForFile SourceMgr;
-  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
   IdentifierTable IdentTable;
   SmallVector<UnwrappedLine, 16> UnwrappedLines;
 };

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 12a985217d973..e87270d638652 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -20,8 +20,10 @@ class TokenAnnotatorTest : public ::testing::Test {
 protected:
   TokenList annotate(llvm::StringRef Code,
                      const FormatStyle &Style = getLLVMStyle()) {
-    return TestLexer(Style).annotate(Code);
+    return TestLexer(Allocator, Buffers, Style).annotate(Code);
   }
+  llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
+  std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
 };
 
 #define EXPECT_TOKEN_KIND(FormatTok, Kind)                                     \


        


More information about the cfe-commits mailing list