1 //===-- clang-import-test.cpp - ASTImporter/ExternalASTSource testbed -----===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/AST/ASTContext.h" 10 #include "clang/AST/ASTImporter.h" 11 #include "clang/AST/DeclObjC.h" 12 #include "clang/AST/ExternalASTMerger.h" 13 #include "clang/Basic/Builtins.h" 14 #include "clang/Basic/IdentifierTable.h" 15 #include "clang/Basic/SourceLocation.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "clang/Basic/TargetOptions.h" 18 #include "clang/CodeGen/ModuleBuilder.h" 19 #include "clang/Driver/Types.h" 20 #include "clang/Frontend/ASTConsumers.h" 21 #include "clang/Frontend/CompilerInstance.h" 22 #include "clang/Frontend/MultiplexConsumer.h" 23 #include "clang/Frontend/TextDiagnosticBuffer.h" 24 #include "clang/Lex/Lexer.h" 25 #include "clang/Lex/Preprocessor.h" 26 #include "clang/Parse/ParseAST.h" 27 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/Module.h" 30 #include "llvm/Support/CommandLine.h" 31 #include "llvm/Support/Error.h" 32 #include "llvm/Support/Host.h" 33 #include "llvm/Support/Signals.h" 34 35 #include <memory> 36 #include <string> 37 38 using namespace clang; 39 40 static llvm::cl::opt<std::string> Expression( 41 "expression", llvm::cl::Required, 42 llvm::cl::desc("Path to a file containing the expression to parse")); 43 44 static llvm::cl::list<std::string> 45 Imports("import", llvm::cl::ZeroOrMore, 46 llvm::cl::desc("Path to a file containing declarations to import")); 47 48 static llvm::cl::opt<bool> 49 Direct("direct", llvm::cl::Optional, 50 llvm::cl::desc("Use the parsed declarations without indirection")); 51 52 static llvm::cl::opt<bool> UseOrigins( 53 "use-origins", llvm::cl::Optional, 54 llvm::cl::desc( 55 "Use DeclContext origin information for more accurate lookups")); 56 57 static llvm::cl::list<std::string> 58 ClangArgs("Xcc", llvm::cl::ZeroOrMore, 59 llvm::cl::desc("Argument to pass to the CompilerInvocation"), 60 llvm::cl::CommaSeparated); 61 62 static llvm::cl::opt<std::string> 63 Input("x", llvm::cl::Optional, 64 llvm::cl::desc("The language to parse (default: c++)"), 65 llvm::cl::init("c++")); 66 67 static llvm::cl::opt<bool> ObjCARC("objc-arc", llvm::cl::init(false), 68 llvm::cl::desc("Emable ObjC ARC")); 69 70 static llvm::cl::opt<bool> DumpAST("dump-ast", llvm::cl::init(false), 71 llvm::cl::desc("Dump combined AST")); 72 73 static llvm::cl::opt<bool> DumpIR("dump-ir", llvm::cl::init(false), 74 llvm::cl::desc("Dump IR from final parse")); 75 76 namespace init_convenience { 77 class TestDiagnosticConsumer : public DiagnosticConsumer { 78 private: 79 std::unique_ptr<TextDiagnosticBuffer> Passthrough; 80 const LangOptions *LangOpts = nullptr; 81 82 public: 83 TestDiagnosticConsumer() 84 : Passthrough(std::make_unique<TextDiagnosticBuffer>()) {} 85 86 virtual void BeginSourceFile(const LangOptions &LangOpts, 87 const Preprocessor *PP = nullptr) override { 88 this->LangOpts = &LangOpts; 89 return Passthrough->BeginSourceFile(LangOpts, PP); 90 } 91 92 virtual void EndSourceFile() override { 93 this->LangOpts = nullptr; 94 Passthrough->EndSourceFile(); 95 } 96 97 virtual bool IncludeInDiagnosticCounts() const override { 98 return Passthrough->IncludeInDiagnosticCounts(); 99 } 100 101 private: 102 static void PrintSourceForLocation(const SourceLocation &Loc, 103 SourceManager &SM) { 104 const char *LocData = SM.getCharacterData(Loc, /*Invalid=*/nullptr); 105 unsigned LocColumn = 106 SM.getSpellingColumnNumber(Loc, /*Invalid=*/nullptr) - 1; 107 FileID FID = SM.getFileID(Loc); 108 const llvm::MemoryBuffer *Buffer = 109 SM.getBuffer(FID, Loc, /*Invalid=*/nullptr); 110 111 assert(LocData >= Buffer->getBufferStart() && 112 LocData < Buffer->getBufferEnd()); 113 114 const char *LineBegin = LocData - LocColumn; 115 116 assert(LineBegin >= Buffer->getBufferStart()); 117 118 const char *LineEnd = nullptr; 119 120 for (LineEnd = LineBegin; *LineEnd != '\n' && *LineEnd != '\r' && 121 LineEnd < Buffer->getBufferEnd(); 122 ++LineEnd) 123 ; 124 125 llvm::StringRef LineString(LineBegin, LineEnd - LineBegin); 126 127 llvm::errs() << LineString << '\n'; 128 llvm::errs().indent(LocColumn); 129 llvm::errs() << '^'; 130 llvm::errs() << '\n'; 131 } 132 133 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 134 const Diagnostic &Info) override { 135 if (Info.hasSourceManager() && LangOpts) { 136 SourceManager &SM = Info.getSourceManager(); 137 138 if (Info.getLocation().isValid()) { 139 Info.getLocation().print(llvm::errs(), SM); 140 llvm::errs() << ": "; 141 } 142 143 SmallString<16> DiagText; 144 Info.FormatDiagnostic(DiagText); 145 llvm::errs() << DiagText << '\n'; 146 147 if (Info.getLocation().isValid()) { 148 PrintSourceForLocation(Info.getLocation(), SM); 149 } 150 151 for (const CharSourceRange &Range : Info.getRanges()) { 152 bool Invalid = true; 153 StringRef Ref = Lexer::getSourceText(Range, SM, *LangOpts, &Invalid); 154 if (!Invalid) { 155 llvm::errs() << Ref << '\n'; 156 } 157 } 158 } 159 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); 160 } 161 }; 162 163 std::unique_ptr<CompilerInstance> BuildCompilerInstance() { 164 auto Ins = std::make_unique<CompilerInstance>(); 165 auto DC = std::make_unique<TestDiagnosticConsumer>(); 166 const bool ShouldOwnClient = true; 167 Ins->createDiagnostics(DC.release(), ShouldOwnClient); 168 169 auto Inv = std::make_unique<CompilerInvocation>(); 170 171 std::vector<const char *> ClangArgv(ClangArgs.size()); 172 std::transform(ClangArgs.begin(), ClangArgs.end(), ClangArgv.begin(), 173 [](const std::string &s) -> const char * { return s.data(); }); 174 CompilerInvocation::CreateFromArgs(*Inv, ClangArgv, Ins->getDiagnostics()); 175 176 { 177 using namespace driver::types; 178 ID Id = lookupTypeForTypeSpecifier(Input.c_str()); 179 assert(Id != TY_INVALID); 180 if (isCXX(Id)) { 181 Inv->getLangOpts()->CPlusPlus = true; 182 Inv->getLangOpts()->CPlusPlus11 = true; 183 Inv->getHeaderSearchOpts().UseLibcxx = true; 184 } 185 if (isObjC(Id)) { 186 Inv->getLangOpts()->ObjC = 1; 187 } 188 } 189 Inv->getLangOpts()->ObjCAutoRefCount = ObjCARC; 190 191 Inv->getLangOpts()->Bool = true; 192 Inv->getLangOpts()->WChar = true; 193 Inv->getLangOpts()->Blocks = true; 194 Inv->getLangOpts()->DebuggerSupport = true; 195 Inv->getLangOpts()->SpellChecking = false; 196 Inv->getLangOpts()->ThreadsafeStatics = false; 197 Inv->getLangOpts()->AccessControl = false; 198 Inv->getLangOpts()->DollarIdents = true; 199 Inv->getLangOpts()->Exceptions = true; 200 Inv->getLangOpts()->CXXExceptions = true; 201 // Needed for testing dynamic_cast. 202 Inv->getLangOpts()->RTTI = true; 203 Inv->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); 204 Inv->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); 205 206 Ins->setInvocation(std::move(Inv)); 207 208 TargetInfo *TI = TargetInfo::CreateTargetInfo( 209 Ins->getDiagnostics(), Ins->getInvocation().TargetOpts); 210 Ins->setTarget(TI); 211 Ins->getTarget().adjust(Ins->getLangOpts()); 212 Ins->createFileManager(); 213 Ins->createSourceManager(Ins->getFileManager()); 214 Ins->createPreprocessor(TU_Complete); 215 216 return Ins; 217 } 218 219 std::unique_ptr<ASTContext> 220 BuildASTContext(CompilerInstance &CI, SelectorTable &ST, Builtin::Context &BC) { 221 auto AST = std::make_unique<ASTContext>( 222 CI.getLangOpts(), CI.getSourceManager(), 223 CI.getPreprocessor().getIdentifierTable(), ST, BC); 224 AST->InitBuiltinTypes(CI.getTarget()); 225 return AST; 226 } 227 228 std::unique_ptr<CodeGenerator> BuildCodeGen(CompilerInstance &CI, 229 llvm::LLVMContext &LLVMCtx) { 230 StringRef ModuleName("$__module"); 231 return std::unique_ptr<CodeGenerator>(CreateLLVMCodeGen( 232 CI.getDiagnostics(), ModuleName, CI.getHeaderSearchOpts(), 233 CI.getPreprocessorOpts(), CI.getCodeGenOpts(), LLVMCtx)); 234 } 235 } // namespace init_convenience 236 237 namespace { 238 239 /// A container for a CompilerInstance (possibly with an ExternalASTMerger 240 /// attached to its ASTContext). 241 /// 242 /// Provides an accessor for the DeclContext origins associated with the 243 /// ExternalASTMerger (or an empty list of origins if no ExternalASTMerger is 244 /// attached). 245 /// 246 /// This is the main unit of parsed source code maintained by clang-import-test. 247 struct CIAndOrigins { 248 using OriginMap = clang::ExternalASTMerger::OriginMap; 249 std::unique_ptr<CompilerInstance> CI; 250 251 ASTContext &getASTContext() { return CI->getASTContext(); } 252 FileManager &getFileManager() { return CI->getFileManager(); } 253 const OriginMap &getOriginMap() { 254 static const OriginMap EmptyOriginMap{}; 255 if (ExternalASTSource *Source = CI->getASTContext().getExternalSource()) 256 return static_cast<ExternalASTMerger *>(Source)->GetOrigins(); 257 return EmptyOriginMap; 258 } 259 DiagnosticConsumer &getDiagnosticClient() { 260 return CI->getDiagnosticClient(); 261 } 262 CompilerInstance &getCompilerInstance() { return *CI; } 263 }; 264 265 void AddExternalSource(CIAndOrigins &CI, 266 llvm::MutableArrayRef<CIAndOrigins> Imports) { 267 ExternalASTMerger::ImporterTarget Target( 268 {CI.getASTContext(), CI.getFileManager()}); 269 llvm::SmallVector<ExternalASTMerger::ImporterSource, 3> Sources; 270 for (CIAndOrigins &Import : Imports) 271 Sources.emplace_back(Import.getASTContext(), Import.getFileManager(), 272 Import.getOriginMap()); 273 auto ES = std::make_unique<ExternalASTMerger>(Target, Sources); 274 CI.getASTContext().setExternalSource(ES.release()); 275 CI.getASTContext().getTranslationUnitDecl()->setHasExternalVisibleStorage(); 276 } 277 278 CIAndOrigins BuildIndirect(CIAndOrigins &CI) { 279 CIAndOrigins IndirectCI{init_convenience::BuildCompilerInstance()}; 280 auto ST = std::make_unique<SelectorTable>(); 281 auto BC = std::make_unique<Builtin::Context>(); 282 std::unique_ptr<ASTContext> AST = init_convenience::BuildASTContext( 283 IndirectCI.getCompilerInstance(), *ST, *BC); 284 IndirectCI.getCompilerInstance().setASTContext(AST.release()); 285 AddExternalSource(IndirectCI, CI); 286 return IndirectCI; 287 } 288 289 llvm::Error ParseSource(const std::string &Path, CompilerInstance &CI, 290 ASTConsumer &Consumer) { 291 SourceManager &SM = CI.getSourceManager(); 292 auto FE = CI.getFileManager().getFile(Path); 293 if (!FE) { 294 return llvm::make_error<llvm::StringError>( 295 llvm::Twine("Couldn't open ", Path), std::error_code()); 296 } 297 SM.setMainFileID(SM.createFileID(*FE, SourceLocation(), SrcMgr::C_User)); 298 ParseAST(CI.getPreprocessor(), &Consumer, CI.getASTContext()); 299 return llvm::Error::success(); 300 } 301 302 llvm::Expected<CIAndOrigins> Parse(const std::string &Path, 303 llvm::MutableArrayRef<CIAndOrigins> Imports, 304 bool ShouldDumpAST, bool ShouldDumpIR) { 305 CIAndOrigins CI{init_convenience::BuildCompilerInstance()}; 306 auto ST = std::make_unique<SelectorTable>(); 307 auto BC = std::make_unique<Builtin::Context>(); 308 std::unique_ptr<ASTContext> AST = 309 init_convenience::BuildASTContext(CI.getCompilerInstance(), *ST, *BC); 310 CI.getCompilerInstance().setASTContext(AST.release()); 311 if (Imports.size()) 312 AddExternalSource(CI, Imports); 313 314 std::vector<std::unique_ptr<ASTConsumer>> ASTConsumers; 315 316 auto LLVMCtx = std::make_unique<llvm::LLVMContext>(); 317 ASTConsumers.push_back( 318 init_convenience::BuildCodeGen(CI.getCompilerInstance(), *LLVMCtx)); 319 auto &CG = *static_cast<CodeGenerator *>(ASTConsumers.back().get()); 320 321 if (ShouldDumpAST) 322 ASTConsumers.push_back( 323 CreateASTDumper(nullptr /*Dump to stdout.*/, "", true, false, false, 324 clang::ADOF_Default)); 325 326 CI.getDiagnosticClient().BeginSourceFile( 327 CI.getCompilerInstance().getLangOpts(), 328 &CI.getCompilerInstance().getPreprocessor()); 329 MultiplexConsumer Consumers(std::move(ASTConsumers)); 330 Consumers.Initialize(CI.getASTContext()); 331 332 if (llvm::Error PE = ParseSource(Path, CI.getCompilerInstance(), Consumers)) 333 return std::move(PE); 334 CI.getDiagnosticClient().EndSourceFile(); 335 if (ShouldDumpIR) 336 CG.GetModule()->print(llvm::outs(), nullptr); 337 if (CI.getDiagnosticClient().getNumErrors()) 338 return llvm::make_error<llvm::StringError>( 339 "Errors occurred while parsing the expression.", std::error_code()); 340 return std::move(CI); 341 } 342 343 void Forget(CIAndOrigins &CI, llvm::MutableArrayRef<CIAndOrigins> Imports) { 344 llvm::SmallVector<ExternalASTMerger::ImporterSource, 3> Sources; 345 for (CIAndOrigins &Import : Imports) 346 Sources.push_back({Import.getASTContext(), Import.getFileManager(), 347 Import.getOriginMap()}); 348 ExternalASTSource *Source = CI.CI->getASTContext().getExternalSource(); 349 auto *Merger = static_cast<ExternalASTMerger *>(Source); 350 Merger->RemoveSources(Sources); 351 } 352 353 } // end namespace 354 355 int main(int argc, const char **argv) { 356 const bool DisableCrashReporting = true; 357 llvm::sys::PrintStackTraceOnErrorSignal(argv[0], DisableCrashReporting); 358 llvm::cl::ParseCommandLineOptions(argc, argv); 359 std::vector<CIAndOrigins> ImportCIs; 360 for (auto I : Imports) { 361 llvm::Expected<CIAndOrigins> ImportCI = Parse(I, {}, false, false); 362 if (auto E = ImportCI.takeError()) { 363 llvm::errs() << llvm::toString(std::move(E)); 364 exit(-1); 365 } 366 ImportCIs.push_back(std::move(*ImportCI)); 367 } 368 std::vector<CIAndOrigins> IndirectCIs; 369 if (!Direct || UseOrigins) { 370 for (auto &ImportCI : ImportCIs) { 371 CIAndOrigins IndirectCI = BuildIndirect(ImportCI); 372 IndirectCIs.push_back(std::move(IndirectCI)); 373 } 374 } 375 if (UseOrigins) 376 for (auto &ImportCI : ImportCIs) 377 IndirectCIs.push_back(std::move(ImportCI)); 378 llvm::Expected<CIAndOrigins> ExpressionCI = 379 Parse(Expression, (Direct && !UseOrigins) ? ImportCIs : IndirectCIs, 380 DumpAST, DumpIR); 381 if (auto E = ExpressionCI.takeError()) { 382 llvm::errs() << llvm::toString(std::move(E)); 383 exit(-1); 384 } 385 Forget(*ExpressionCI, (Direct && !UseOrigins) ? ImportCIs : IndirectCIs); 386 return 0; 387 } 388