Instantiator
Generate c++ template instantiations
Loading...
Searching...
No Matches
ASTBuilderAction.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4
5#include "fmt/std.h"
6#include "spdlog/spdlog.h"
7
8#include "clang/Basic/Diagnostic.h"
9#include "clang/Basic/DiagnosticIDs.h"
10#include "clang/Basic/FileManager.h"
11#include "llvm/ADT/IntrusiveRefCntPtr.h"
12
13#include "ASTCreation.hpp"
14
15bool ASTBuilderAction::runInvocation(std::shared_ptr<clang::CompilerInvocation> Invocation,
16 clang::FileManager* Files,
17 std::shared_ptr<clang::PCHContainerOperations> PCHContainerOps,
18 clang::DiagnosticConsumer* DiagConsumer)
19{
20 spdlog::debug("Processing {}", file);
21 bool is_cached_on_disk = internal::is_cached(db, file, tmpdir);
22 spdlog::debug("cached={}", is_cached_on_disk);
23#if INSTANTIATOR_LLVM_MAJOR > 19
24 auto diag_ids = llvm::makeIntrusiveRefCnt<clang::DiagnosticIDs>();
25 auto diag_engine = clang::DiagnosticsEngine(diag_ids, Invocation->DiagnosticOpts);
26#endif
27 if(is_cached_on_disk) {
28 clang::CompilerInstance CI;
29 CI.setInvocation(Invocation);
30#if INSTANTIATOR_LLVM_MAJOR < 20
31 CI.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
32#else
33 CI.createDiagnostics(*createVFSFromCompilerInvocation(*Invocation, diag_engine), DiagConsumer, /*ShouldOwnClient=*/false);
34#endif
35
36 clang::DiagnosticsEngine* DiagEngine = &CI.getDiagnostics();
37#if INSTANTIATOR_LLVM_MAJOR > 16
38 AST = clang::ASTUnit::LoadFromASTFile((tmpdir / file.filename().replace_extension("ast")).string(),
39 CI.getPCHContainerReader(),
40 clang::ASTUnit::WhatToLoad::LoadEverything,
41 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>(DiagEngine),
42 CI.getFileSystemOpts(),
43 CI.getHeaderSearchOptsPtr());
44#else
45 AST = clang::ASTUnit::LoadFromASTFile((tmpdir / file.filename().replace_extension("ast")).string(),
46 CI.getPCHContainerReader(),
47 clang::ASTUnit::WhatToLoad::LoadEverything,
48 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine>(DiagEngine),
49 CI.getFileSystemOpts());
50#endif
51 } else {
52#if INSTANTIATOR_LLVM_MAJOR < 20
53 AST = clang::ASTUnit::LoadFromCompilerInvocation(Invocation,
54 std::move(PCHContainerOps),
55 clang::CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
56 DiagConsumer,
57 /*ShouldOwnlient=*/false),
58 Files);
59#else
60 AST = clang::ASTUnit::LoadFromCompilerInvocation(
61 Invocation,
62 std::move(PCHContainerOps),
63 clang::CompilerInstance::createDiagnostics(*createVFSFromCompilerInvocation(*Invocation, diag_engine),
64 &Invocation->getDiagnosticOpts(),
65 DiagConsumer,
66 /*ShouldOwnClient=*/false),
67 Files);
68#endif
69 AST->Save((tmpdir / file.filename().replace_extension("ast")).string());
70 }
71 if(!AST or AST->getDiagnostics().hasUncompilableErrorOccurred()) return false;
72 return true;
73}
const clang::tooling::CompilationDatabase & db
std::filesystem::path file
std::filesystem::path tmpdir
std::unique_ptr< clang::ASTUnit > & AST
bool runInvocation(std::shared_ptr< clang::CompilerInvocation > Invocation, clang::FileManager *Files, std::shared_ptr< clang::PCHContainerOperations > PCHContainerOps, clang::DiagnosticConsumer *DiagConsumer) override
bool is_cached(const clang::tooling::CompilationDatabase &db, const std::filesystem::path &filename, const std::filesystem::path &tmpdir)