[clang] 6c185ac - Revert "Move the test compiler setup in a common place. NFCI"

Vitaly Buka via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 16 04:21:47 PST 2020


Author: Vitaly Buka
Date: 2020-11-16T04:21:28-08:00
New Revision: 6c185acfffc1f7cdf2a6e785cb4a422140c34f06

URL: https://github.com/llvm/llvm-project/commit/6c185acfffc1f7cdf2a6e785cb4a422140c34f06
DIFF: https://github.com/llvm/llvm-project/commit/6c185acfffc1f7cdf2a6e785cb4a422140c34f06.diff

LOG: Revert "Move the test compiler setup in a common place. NFCI"

There is memory leaks

This reverts commit 23cc838099e10b13a32e54105f4db0f1b7e3a842.
This reverts commit 888d06dfb8b55c4fd41fa4febe22c6fc4111c118.

Added: 
    

Modified: 
    clang/unittests/CodeGen/BufferSourceTest.cpp
    clang/unittests/CodeGen/CodeGenExternalTest.cpp
    clang/unittests/CodeGen/IncrementalProcessingTest.cpp
    clang/unittests/CodeGen/TBAAMetadataTest.cpp

Removed: 
    clang/unittests/CodeGen/TestCompiler.h


################################################################################
diff  --git a/clang/unittests/CodeGen/BufferSourceTest.cpp b/clang/unittests/CodeGen/BufferSourceTest.cpp
index 3ed688a72185..c1c2bf818c02 100644
--- a/clang/unittests/CodeGen/BufferSourceTest.cpp
+++ b/clang/unittests/CodeGen/BufferSourceTest.cpp
@@ -6,8 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "TestCompiler.h"
-
+#include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/TargetInfo.h"
@@ -27,11 +26,10 @@ using namespace clang;
 
 namespace {
 
-TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
-  // Emitting constructors for global objects involves looking
-  // at the source file name. This makes sure that we don't crash
-  // if the source file is a memory buffer.
-  const char TestProgram[] =
+// Emitting constructors for global objects involves looking
+// at the source file name. This makes sure that we don't crash
+// if the source file is a memory buffer.
+const char TestProgram[] =
     "class EmitCXXGlobalInitFunc    "
     "{                              "
     "public:                        "
@@ -39,13 +37,43 @@ TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
     "};                             "
     "EmitCXXGlobalInitFunc test;    ";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TestCompiler Compiler(LO);
-  Compiler.init(TestProgram);
+TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
+    LLVMContext Context;
+    CompilerInstance compiler;
+
+    compiler.createDiagnostics();
+    compiler.getLangOpts().CPlusPlus = 1;
+    compiler.getLangOpts().CPlusPlus11 = 1;
+
+    compiler.getTargetOpts().Triple = llvm::Triple::normalize(
+        llvm::sys::getProcessTriple());
+    compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
+      compiler.getDiagnostics(),
+      std::make_shared<clang::TargetOptions>(
+        compiler.getTargetOpts())));
+
+    compiler.createFileManager();
+    compiler.createSourceManager(compiler.getFileManager());
+    compiler.createPreprocessor(clang::TU_Prefix);
+
+    compiler.createASTContext();
+
+    compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
+        CreateLLVMCodeGen(
+            compiler.getDiagnostics(),
+            "EmitCXXGlobalInitFuncTest",
+            compiler.getHeaderSearchOpts(),
+            compiler.getPreprocessorOpts(),
+            compiler.getCodeGenOpts(),
+            Context)));
+
+    compiler.createSema(clang::TU_Prefix, nullptr);
+
+    clang::SourceManager &sm = compiler.getSourceManager();
+    sm.setMainFileID(sm.createFileID(
+        llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
 
-  clang::ParseAST(Compiler.compiler.getSema(), false, false);
+    clang::ParseAST(compiler.getSema(), false, false);
 }
 
 } // end anonymous namespace

diff  --git a/clang/unittests/CodeGen/CodeGenExternalTest.cpp b/clang/unittests/CodeGen/CodeGenExternalTest.cpp
index bb5f7463e9b1..255b8c3e9d8c 100644
--- a/clang/unittests/CodeGen/CodeGenExternalTest.cpp
+++ b/clang/unittests/CodeGen/CodeGenExternalTest.cpp
@@ -6,8 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "TestCompiler.h"
-
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/GlobalDecl.h"
@@ -52,11 +50,11 @@ static bool test_codegen_fns_ran;
 // before forwarding that function to the CodeGenerator.
 
 struct MyASTConsumer : public ASTConsumer {
-  CodeGenerator* Builder;
+  std::unique_ptr<CodeGenerator> Builder;
   std::vector<Decl*> toplevel_decls;
 
-  MyASTConsumer(CodeGenerator* Builder_in)
-    : ASTConsumer(), Builder(Builder_in)
+  MyASTConsumer(std::unique_ptr<CodeGenerator> Builder_in)
+    : ASTConsumer(), Builder(std::move(Builder_in))
   {
   }
 
@@ -259,17 +257,45 @@ static void test_codegen_fns(MyASTConsumer *my) {
 }
 
 TEST(CodeGenExternalTest, CodeGenExternalTest) {
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TestCompiler Compiler(LO);
-  auto CustomASTConsumer = std::make_unique<MyASTConsumer>(Compiler.CG);
+    LLVMContext Context;
+    CompilerInstance compiler;
+
+    compiler.createDiagnostics();
+    compiler.getLangOpts().CPlusPlus = 1;
+    compiler.getLangOpts().CPlusPlus11 = 1;
+
+    compiler.getTargetOpts().Triple = llvm::Triple::normalize(
+        llvm::sys::getProcessTriple());
+    compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
+      compiler.getDiagnostics(),
+      std::make_shared<clang::TargetOptions>(
+        compiler.getTargetOpts())));
+
+    compiler.createFileManager();
+    compiler.createSourceManager(compiler.getFileManager());
+    compiler.createPreprocessor(clang::TU_Prefix);
+
+    compiler.createASTContext();
+
+
+    compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
+          new MyASTConsumer(std::unique_ptr<CodeGenerator>(
+             CreateLLVMCodeGen(compiler.getDiagnostics(),
+                               "MemoryTypesTest",
+                               compiler.getHeaderSearchOpts(),
+                               compiler.getPreprocessorOpts(),
+                               compiler.getCodeGenOpts(),
+                               Context)))));
+
+    compiler.createSema(clang::TU_Prefix, nullptr);
 
-  Compiler.init(TestProgram, std::move(CustomASTConsumer));
+    clang::SourceManager &sm = compiler.getSourceManager();
+    sm.setMainFileID(sm.createFileID(
+        llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
 
-  clang::ParseAST(Compiler.compiler.getSema(), false, false);
+    clang::ParseAST(compiler.getSema(), false, false);
 
-  ASSERT_TRUE(test_codegen_fns_ran);
+    ASSERT_TRUE(test_codegen_fns_ran);
 }
 
 } // end anonymous namespace

diff  --git a/clang/unittests/CodeGen/IncrementalProcessingTest.cpp b/clang/unittests/CodeGen/IncrementalProcessingTest.cpp
index 2fb486fd6855..045ed9bbc760 100644
--- a/clang/unittests/CodeGen/IncrementalProcessingTest.cpp
+++ b/clang/unittests/CodeGen/IncrementalProcessingTest.cpp
@@ -6,8 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "TestCompiler.h"
-
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecursiveASTVisitor.h"
@@ -110,30 +108,52 @@ const Function* getGlobalInit(llvm::Module& M) {
 }
 
 TEST(IncrementalProcessing, EmitCXXGlobalInitFunc) {
-    clang::LangOptions LO;
-    LO.CPlusPlus = 1;
-    LO.CPlusPlus11 = 1;
-    TestCompiler Compiler(LO);
-    clang::CompilerInstance &CI = Compiler.compiler;
-    CI.getPreprocessor().enableIncrementalProcessing();
-    CI.setASTConsumer(std::unique_ptr<ASTConsumer>(Compiler.CG));
-    CI.createSema(clang::TU_Prefix, nullptr);
-
-    Sema& S = CI.getSema();
+    LLVMContext Context;
+    CompilerInstance compiler;
+
+    compiler.createDiagnostics();
+    compiler.getLangOpts().CPlusPlus = 1;
+    compiler.getLangOpts().CPlusPlus11 = 1;
+
+    compiler.getTargetOpts().Triple = llvm::Triple::normalize(
+        llvm::sys::getProcessTriple());
+    compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
+      compiler.getDiagnostics(),
+      std::make_shared<clang::TargetOptions>(
+        compiler.getTargetOpts())));
+
+    compiler.createFileManager();
+    compiler.createSourceManager(compiler.getFileManager());
+    compiler.createPreprocessor(clang::TU_Prefix);
+    compiler.getPreprocessor().enableIncrementalProcessing();
+
+    compiler.createASTContext();
+
+    CodeGenerator* CG =
+        CreateLLVMCodeGen(
+            compiler.getDiagnostics(),
+            "main-module",
+            compiler.getHeaderSearchOpts(),
+            compiler.getPreprocessorOpts(),
+            compiler.getCodeGenOpts(),
+            Context);
+    compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(CG));
+    compiler.createSema(clang::TU_Prefix, nullptr);
+    Sema& S = compiler.getSema();
 
     std::unique_ptr<Parser> ParseOP(new Parser(S.getPreprocessor(), S,
                                                /*SkipFunctionBodies*/ false));
     Parser &P = *ParseOP.get();
 
     std::array<std::unique_ptr<llvm::Module>, 3> M;
-    M[0] = IncrementalParseAST(CI, P, *Compiler.CG, nullptr);
+    M[0] = IncrementalParseAST(compiler, P, *CG, nullptr);
     ASSERT_TRUE(M[0]);
 
-    M[1] = IncrementalParseAST(CI, P, *Compiler.CG, TestProgram1);
+    M[1] = IncrementalParseAST(compiler, P, *CG, TestProgram1);
     ASSERT_TRUE(M[1]);
     ASSERT_TRUE(M[1]->getFunction("funcForProg1"));
 
-    M[2] = IncrementalParseAST(CI, P, *Compiler.CG, TestProgram2);
+    M[2] = IncrementalParseAST(compiler, P, *CG, TestProgram2);
     ASSERT_TRUE(M[2]);
     ASSERT_TRUE(M[2]->getFunction("funcForProg2"));
     // First code should not end up in second module:

diff  --git a/clang/unittests/CodeGen/TBAAMetadataTest.cpp b/clang/unittests/CodeGen/TBAAMetadataTest.cpp
index 149a8e074b69..feea8641a5e9 100644
--- a/clang/unittests/CodeGen/TBAAMetadataTest.cpp
+++ b/clang/unittests/CodeGen/TBAAMetadataTest.cpp
@@ -7,12 +7,18 @@
 //===----------------------------------------------------------------------===//
 
 #include "IRMatchers.h"
-#include "TestCompiler.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/CodeGen/ModuleBuilder.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Parse/ParseAST.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "gtest/gtest.h"
 #include <memory>
@@ -21,17 +27,82 @@ using namespace llvm;
 
 namespace {
 
-struct TBAATestCompiler : public TestCompiler {
-  TBAATestCompiler(clang::LangOptions LO, clang::CodeGenOptions CGO)
-    : TestCompiler(LO, CGO) {}
-  static clang::CodeGenOptions getCommonCodeGenOpts() {
-    clang::CodeGenOptions CGOpts;
-    CGOpts.StructPathTBAA = 1;
-    CGOpts.OptimizationLevel = 1;
-    return CGOpts;
+struct TestCompiler {
+  LLVMContext Context;
+  clang::CompilerInstance compiler;
+  clang::CodeGenerator *CG = nullptr;
+  llvm::Module *M = nullptr;
+  unsigned PtrSize = 0;
+
+  void init(const char *TestProgram) {
+    compiler.createDiagnostics();
+    compiler.getCodeGenOpts().StructPathTBAA = 1;
+    compiler.getCodeGenOpts().OptimizationLevel = 1;
+
+    std::string TrStr = llvm::Triple::normalize(llvm::sys::getProcessTriple());
+    llvm::Triple Tr(TrStr);
+    Tr.setOS(Triple::Linux);
+    Tr.setVendor(Triple::VendorType::UnknownVendor);
+    Tr.setEnvironment(Triple::EnvironmentType::UnknownEnvironment);
+    compiler.getTargetOpts().Triple = Tr.getTriple();
+    compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
+        compiler.getDiagnostics(),
+        std::make_shared<clang::TargetOptions>(compiler.getTargetOpts())));
+
+    const clang::TargetInfo &TInfo = compiler.getTarget();
+    PtrSize = TInfo.getPointerWidth(0) / 8;
+
+    compiler.createFileManager();
+    compiler.createSourceManager(compiler.getFileManager());
+    compiler.createPreprocessor(clang::TU_Prefix);
+
+    compiler.createASTContext();
+
+    CG = CreateLLVMCodeGen(
+        compiler.getDiagnostics(),
+        "main-module",
+        compiler.getHeaderSearchOpts(),
+        compiler.getPreprocessorOpts(),
+        compiler.getCodeGenOpts(),
+        Context);
+    compiler.setASTConsumer(std::unique_ptr<clang::ASTConsumer>(CG));
+
+    compiler.createSema(clang::TU_Prefix, nullptr);
+
+    clang::SourceManager &sm = compiler.getSourceManager();
+    sm.setMainFileID(sm.createFileID(
+        llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
+  }
+
+  const BasicBlock *compile() {
+    clang::ParseAST(compiler.getSema(), false, false);
+    M = CG->GetModule();
+
+    // Do not expect more than one function definition.
+    auto FuncPtr = M->begin();
+    for (; FuncPtr != M->end(); ++FuncPtr)
+      if (!FuncPtr->isDeclaration())
+        break;
+    assert(FuncPtr != M->end());
+    const llvm::Function &Func = *FuncPtr;
+    ++FuncPtr;
+    for (; FuncPtr != M->end(); ++FuncPtr)
+      if (!FuncPtr->isDeclaration())
+        break;
+    assert(FuncPtr == M->end());
+
+    // The function must consist of single basic block.
+    auto BBPtr = Func.begin();
+    assert(Func.begin() != Func.end());
+    const BasicBlock &BB = *BBPtr;
+    ++BBPtr;
+    assert(BBPtr == Func.end());
+
+    return &BB;
   }
 };
 
+
 auto OmnipotentCharC = MMTuple(
   MMString("omnipotent char"),
   MMTuple(
@@ -61,8 +132,8 @@ TEST(TBAAMetadataTest, BasicTypes) {
     }
   )**";
 
-  clang::LangOptions LO;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().C11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -157,9 +228,8 @@ TEST(TBAAMetadataTest, CFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.C11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().C11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -279,9 +349,8 @@ TEST(TBAAMetadataTest, CTypedefFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.C11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().C11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -372,9 +441,8 @@ TEST(TBAAMetadataTest, CTypedefFields2) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.C11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().C11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -466,9 +534,8 @@ TEST(TBAAMetadataTest, CTypedefFields3) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.C11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().C11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -568,10 +635,9 @@ TEST(TBAAMetadataTest, CXXFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -691,10 +757,9 @@ TEST(TBAAMetadataTest, CXXTypedefFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -792,10 +857,9 @@ TEST(TBAAMetadataTest, StructureFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -874,10 +938,9 @@ TEST(TBAAMetadataTest, ArrayFields) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -953,10 +1016,9 @@ TEST(TBAAMetadataTest, BaseClass) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -1032,10 +1094,9 @@ TEST(TBAAMetadataTest, PolymorphicClass) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -1109,10 +1170,9 @@ TEST(TBAAMetadataTest, VirtualBase) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 
@@ -1195,10 +1255,9 @@ TEST(TBAAMetadataTest, TemplSpec) {
     }
   )**";
 
-  clang::LangOptions LO;
-  LO.CPlusPlus = 1;
-  LO.CPlusPlus11 = 1;
-  TBAATestCompiler Compiler(LO, TBAATestCompiler::getCommonCodeGenOpts());
+  TestCompiler Compiler;
+  Compiler.compiler.getLangOpts().CPlusPlus = 1;
+  Compiler.compiler.getLangOpts().CPlusPlus11 = 1;
   Compiler.init(TestProgram);
   const BasicBlock *BB = Compiler.compile();
 

diff  --git a/clang/unittests/CodeGen/TestCompiler.h b/clang/unittests/CodeGen/TestCompiler.h
deleted file mode 100644
index 61ca9a3e06fb..000000000000
--- a/clang/unittests/CodeGen/TestCompiler.h
+++ /dev/null
@@ -1,112 +0,0 @@
-//=== unittests/CodeGen/TestCompiler.h - Match on the LLVM IR ---*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H
-#define CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H
-
-
-#include "clang/AST/ASTConsumer.h"
-#include "clang/Basic/TargetInfo.h"
-#include "clang/Basic/TargetOptions.h"
-#include "clang/CodeGen/ModuleBuilder.h"
-#include "clang/Frontend/CompilerInstance.h"
-#include "clang/Parse/ParseAST.h"
-
-#include "llvm/IR/Constants.h"
-#include "llvm/IR/LLVMContext.h"
-#include "llvm/IR/Module.h"
-#include "llvm/Support/Host.h"
-
-namespace llvm {
-
-struct TestCompiler {
-  LLVMContext Context;
-  clang::CompilerInstance compiler;
-  clang::CodeGenerator* CG;
-  llvm::Module *M = nullptr;
-  unsigned PtrSize = 0;
-
-  TestCompiler(clang::LangOptions LO,
-               clang::CodeGenOptions CGO = clang::CodeGenOptions()) {
-    compiler.getLangOpts() = LO;
-    compiler.getCodeGenOpts() = CGO;
-    compiler.createDiagnostics();
-
-    std::string TrStr = llvm::Triple::normalize(llvm::sys::getProcessTriple());
-    llvm::Triple Tr(TrStr);
-    Tr.setOS(Triple::Linux);
-    Tr.setVendor(Triple::VendorType::UnknownVendor);
-    Tr.setEnvironment(Triple::EnvironmentType::UnknownEnvironment);
-    compiler.getTargetOpts().Triple = Tr.getTriple();
-    compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
-        compiler.getDiagnostics(),
-        std::make_shared<clang::TargetOptions>(compiler.getTargetOpts())));
-
-    const clang::TargetInfo &TInfo = compiler.getTarget();
-    PtrSize = TInfo.getPointerWidth(0) / 8;
-
-    compiler.createFileManager();
-    compiler.createSourceManager(compiler.getFileManager());
-    compiler.createPreprocessor(clang::TU_Prefix);
-
-    compiler.createASTContext();
-
-    CG = CreateLLVMCodeGen(compiler.getDiagnostics(),
-                           "main-module",
-                           compiler.getHeaderSearchOpts(),
-                           compiler.getPreprocessorOpts(),
-                           compiler.getCodeGenOpts(),
-                           Context);
-  }
-
-  void init(const char *TestProgram,
-            std::unique_ptr<clang::ASTConsumer> Consumer = nullptr) {
-    assert(CG);
-
-    if (!Consumer)
-      Consumer = std::unique_ptr<clang::ASTConsumer>(CG);
-
-    compiler.setASTConsumer(std::move(Consumer));
-
-    compiler.createSema(clang::TU_Prefix, nullptr);
-
-    clang::SourceManager &sm = compiler.getSourceManager();
-    sm.setMainFileID(sm.createFileID(
-        llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
-  }
-
-  const BasicBlock *compile() {
-    clang::ParseAST(compiler.getSema(), false, false);
-    M = CG->GetModule();
-
-    // Do not expect more than one function definition.
-    auto FuncPtr = M->begin();
-    for (; FuncPtr != M->end(); ++FuncPtr)
-      if (!FuncPtr->isDeclaration())
-        break;
-    assert(FuncPtr != M->end());
-    const llvm::Function &Func = *FuncPtr;
-    ++FuncPtr;
-    for (; FuncPtr != M->end(); ++FuncPtr)
-      if (!FuncPtr->isDeclaration())
-        break;
-    assert(FuncPtr == M->end());
-
-    // The function must consist of single basic block.
-    auto BBPtr = Func.begin();
-    assert(Func.begin() != Func.end());
-    const BasicBlock &BB = *BBPtr;
-    ++BBPtr;
-    assert(BBPtr == Func.end());
-
-    return &BB;
-  }
-};
-
-} // namespace llvm
-#endif // CLANG_UNITTESTS_CODEGEN_TESTCOMPILER_H


        


More information about the cfe-commits mailing list