brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.5 KiB · be3be14 Raw
276 lines · cpp
1//===- unittests/CodeGen/CodeGenExternalTest.cpp - test external CodeGen -===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "TestCompiler.h"10 11#include "clang/AST/ASTConsumer.h"12#include "clang/AST/ASTContext.h"13#include "clang/AST/GlobalDecl.h"14#include "clang/AST/RecursiveASTVisitor.h"15#include "clang/Basic/TargetInfo.h"16#include "clang/CodeGen/CodeGenABITypes.h"17#include "clang/CodeGen/ModuleBuilder.h"18#include "clang/Frontend/CompilerInstance.h"19#include "clang/Lex/Preprocessor.h"20#include "clang/Parse/ParseAST.h"21#include "clang/Sema/Sema.h"22#include "llvm/IR/Instructions.h"23#include "llvm/IR/LLVMContext.h"24#include "llvm/Support/Debug.h"25#include "llvm/Support/MemoryBuffer.h"26#include "llvm/TargetParser/Host.h"27#include "llvm/TargetParser/Triple.h"28#include "gtest/gtest.h"29 30using namespace llvm;31using namespace clang;32 33namespace {34 35// Mocks up a language using Clang code generation as a library and36// tests some basic functionality there.37//   - CodeGen->GetAddrOfGlobal38//   - CodeGen::convertTypeForMemory39//   - CodeGen::getLLVMFieldNumber40 41static const bool DebugThisTest = false;42 43// forward declarations44struct MyASTConsumer;45static void test_codegen_fns(MyASTConsumer *my);46static bool test_codegen_fns_ran;47 48// This forwards the calls to the Clang CodeGenerator49// so that we can test CodeGen functions while it is open.50// It accumulates toplevel decls in HandleTopLevelDecl and51// calls test_codegen_fns() in HandleTranslationUnit52// before forwarding that function to the CodeGenerator.53 54struct MyASTConsumer : public ASTConsumer {55  std::unique_ptr<CodeGenerator> Builder;56  std::vector<Decl*> toplevel_decls;57 58  MyASTConsumer(std::unique_ptr<CodeGenerator> Builder_in)59    : ASTConsumer(), Builder(std::move(Builder_in))60  {61  }62 63  ~MyASTConsumer() { }64 65  void Initialize(ASTContext &Context) override;66  void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override;67  bool HandleTopLevelDecl(DeclGroupRef D) override;68  void HandleInlineFunctionDefinition(FunctionDecl *D) override;69  void HandleInterestingDecl(DeclGroupRef D) override;70  void HandleTranslationUnit(ASTContext &Ctx) override;71  void HandleTagDeclDefinition(TagDecl *D) override;72  void HandleTagDeclRequiredDefinition(const TagDecl *D) override;73  void HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) override;74  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override;75  void HandleImplicitImportDecl(ImportDecl *D) override;76  void CompleteTentativeDefinition(VarDecl *D) override;77  void AssignInheritanceModel(CXXRecordDecl *RD) override;78  void HandleVTable(CXXRecordDecl *RD) override;79  ASTMutationListener *GetASTMutationListener() override;80  ASTDeserializationListener *GetASTDeserializationListener() override;81  void PrintStats() override;82  bool shouldSkipFunctionBody(Decl *D) override;83};84 85void MyASTConsumer::Initialize(ASTContext &Context) {86  Builder->Initialize(Context);87}88 89bool MyASTConsumer::HandleTopLevelDecl(DeclGroupRef DG) {90 91  for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {92    toplevel_decls.push_back(*I);93  }94 95  return Builder->HandleTopLevelDecl(DG);96}97 98void MyASTConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {99  Builder->HandleInlineFunctionDefinition(D);100}101 102void MyASTConsumer::HandleInterestingDecl(DeclGroupRef D) {103  Builder->HandleInterestingDecl(D);104}105 106void MyASTConsumer::HandleTranslationUnit(ASTContext &Context) {107  test_codegen_fns(this);108  // HandleTranslationUnit can close the module109  Builder->HandleTranslationUnit(Context);110}111 112void MyASTConsumer::HandleTagDeclDefinition(TagDecl *D) {113  Builder->HandleTagDeclDefinition(D);114}115 116void MyASTConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {117  Builder->HandleTagDeclRequiredDefinition(D);118}119 120void MyASTConsumer::HandleCXXImplicitFunctionInstantiation(FunctionDecl *D) {121  Builder->HandleCXXImplicitFunctionInstantiation(D);122}123 124void MyASTConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef D) {125  Builder->HandleTopLevelDeclInObjCContainer(D);126}127 128void MyASTConsumer::HandleImplicitImportDecl(ImportDecl *D) {129  Builder->HandleImplicitImportDecl(D);130}131 132void MyASTConsumer::CompleteTentativeDefinition(VarDecl *D) {133  Builder->CompleteTentativeDefinition(D);134}135 136void MyASTConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {137  Builder->AssignInheritanceModel(RD);138}139 140void MyASTConsumer::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {141   Builder->HandleCXXStaticMemberVarInstantiation(VD);142}143 144void MyASTConsumer::HandleVTable(CXXRecordDecl *RD) {145   Builder->HandleVTable(RD);146 }147 148ASTMutationListener *MyASTConsumer::GetASTMutationListener() {149  return Builder->GetASTMutationListener();150}151 152ASTDeserializationListener *MyASTConsumer::GetASTDeserializationListener() {153  return Builder->GetASTDeserializationListener();154}155 156void MyASTConsumer::PrintStats() {157  Builder->PrintStats();158}159 160bool MyASTConsumer::shouldSkipFunctionBody(Decl *D) {161  return Builder->shouldSkipFunctionBody(D);162}163 164const char TestProgram[] =165    "struct mytest_struct { char x; short y; char p; long z; };\n"166    "int mytest_fn(int x) { return x; }\n";167 168// This function has the real test code here169static void test_codegen_fns(MyASTConsumer *my) {170 171  bool mytest_fn_ok = false;172  bool mytest_struct_ok = false;173 174  CodeGen::CodeGenModule &CGM = my->Builder->CGM();175  const ASTContext &Ctx = my->toplevel_decls.front()->getASTContext();176 177  for (auto decl : my->toplevel_decls ) {178    if (FunctionDecl *fd = dyn_cast<FunctionDecl>(decl)) {179      if (fd->getName() == "mytest_fn") {180        Constant *c = my->Builder->GetAddrOfGlobal(GlobalDecl(fd), false);181        // Verify that we got a function.182        ASSERT_TRUE(c != NULL);183        if (DebugThisTest) {184          c->print(dbgs(), true);185          dbgs() << "\n";186        }187        mytest_fn_ok = true;188      }189    } else if(clang::RecordDecl *rd = dyn_cast<RecordDecl>(decl)) {190      if (rd->getName() == "mytest_struct") {191        RecordDecl *def = rd->getDefinition();192        ASSERT_TRUE(def != NULL);193        CanQualType qType = Ctx.getCanonicalTagType(rd);194 195        // Check convertTypeForMemory196        llvm::Type *llvmTy = CodeGen::convertTypeForMemory(CGM, qType);197        ASSERT_TRUE(llvmTy != NULL);198        if (DebugThisTest) {199          llvmTy->print(dbgs(), true);200          dbgs() << "\n";201        }202 203        auto* structTy = dyn_cast<llvm::StructType>(llvmTy);204        ASSERT_TRUE(structTy != NULL);205 206        // Check getLLVMFieldNumber207        FieldDecl *xField = NULL;208        FieldDecl *yField = NULL;209        FieldDecl *zField = NULL;210 211        for (auto field : rd->fields()) {212          if (field->getName() == "x") xField = field;213          if (field->getName() == "y") yField = field;214          if (field->getName() == "z") zField = field;215        }216 217        ASSERT_TRUE(xField != NULL);218        ASSERT_TRUE(yField != NULL);219        ASSERT_TRUE(zField != NULL);220 221        unsigned x = CodeGen::getLLVMFieldNumber(CGM, rd, xField);222        unsigned y = CodeGen::getLLVMFieldNumber(CGM, rd, yField);223        unsigned z = CodeGen::getLLVMFieldNumber(CGM, rd, zField);224 225        ASSERT_NE(x, y);226        ASSERT_NE(y, z);227 228        llvm::Type* xTy = structTy->getTypeAtIndex(x);229        llvm::Type* yTy = structTy->getTypeAtIndex(y);230        llvm::Type* zTy = structTy->getTypeAtIndex(z);231 232        ASSERT_TRUE(xTy != NULL);233        ASSERT_TRUE(yTy != NULL);234        ASSERT_TRUE(zTy != NULL);235 236        if (DebugThisTest) {237          xTy->print(dbgs(), true);238          dbgs() << "\n";239          yTy->print(dbgs(), true);240          dbgs() << "\n";241          zTy->print(dbgs(), true);242          dbgs() << "\n";243        }244 245        ASSERT_GE(xTy->getPrimitiveSizeInBits(), 1u);246        ASSERT_GE(yTy->getPrimitiveSizeInBits(), 16u); // short is at least 16b247        ASSERT_GE(zTy->getPrimitiveSizeInBits(), 32u); // long is at least 32b248 249        mytest_struct_ok = true;250      }251    }252  }253 254  ASSERT_TRUE(mytest_fn_ok);255  ASSERT_TRUE(mytest_struct_ok);256 257  test_codegen_fns_ran = true;258}259 260TEST(CodeGenExternalTest, CodeGenExternalTest) {261  clang::LangOptions LO;262  LO.CPlusPlus = 1;263  LO.CPlusPlus11 = 1;264  TestCompiler Compiler(LO);265  auto CustomASTConsumer266    = std::make_unique<MyASTConsumer>(std::move(Compiler.CG));267 268  Compiler.init(TestProgram, std::move(CustomASTConsumer));269 270  clang::ParseAST(Compiler.compiler.getSema(), false, false);271 272  ASSERT_TRUE(test_codegen_fns_ran);273}274 275} // end anonymous namespace276