| 1 |
//===- ASTUnitHack.cpp - Hacks for the ASTUnit class ------------------*- C++ *-===// |
| 2 |
// |
| 3 |
// This project is licensed 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 |
/// \file |
| 10 |
/// Hacks for the ASTUnit class. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#include <clang/Frontend/ASTUnit.h> |
| 17 |
#include <clang/Frontend/CompilerInvocation.h> |
| 18 |
#include <clang/Serialization/InMemoryModuleCache.h> |
| 19 |
#include <clang/Basic/Version.h> |
| 20 |
|
| 21 |
#if CLANG_VERSION_MAJOR >= 21 |
| 22 |
#include <clang/Serialization/ModuleCache.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
using namespace clang; |
| 26 |
|
| 27 |
/** Common code for the ASTUnit::create functions. */ |
| 28 |
#define ASTUNIT_COMMON(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile) \ |
| 29 |
std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); \ |
| 30 |
ConfigureDiags(Diags, *AST, CaptureDiagnostics); \ |
| 31 |
AST->Diagnostics = Diags; \ |
| 32 |
AST->FileSystemOpts = CI->getFileSystemOpts(); \ |
| 33 |
AST->Invocation = std::move(CI); \ |
| 34 |
AST->FileMgr = new FileManager(AST->FileSystemOpts, _Hack_VFS); \ |
| 35 |
AST->UserFilesAreVolatile = UserFilesAreVolatile; \ |
| 36 |
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr, \ |
| 37 |
UserFilesAreVolatile) |
| 38 |
|
| 39 |
/** Filesystem that will be used in our custom ASTUnit::create, so that way we |
| 40 |
* don't break clang's API. |
| 41 |
*/ |
| 42 |
IntrusiveRefCntPtr<llvm::vfs::FileSystem> _Hack_VFS; |
| 43 |
|
| 44 |
|
| 45 |
/** Hack to create an ASTUnit from LoadFromCompilerInvocationAction with a |
| 46 |
* virtual filesystem. That way we can use FrontendActions hooks when |
| 47 |
* creating the ASTUnit. |
| 48 |
*/ |
| 49 |
#if CLANG_VERSION_MAJOR >= 21 |
| 50 |
std::unique_ptr<ASTUnit> |
| 51 |
ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, |
| 52 |
std::shared_ptr<DiagnosticOptions> DiagOpts, |
| 53 |
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
| 54 |
CaptureDiagsKind CaptureDiagnostics, |
| 55 |
bool UserFilesAreVolatile) { |
| 56 |
|
| 57 |
ASTUNIT_COMMON(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); |
| 58 |
AST->DiagOpts = DiagOpts; |
| 59 |
AST->ModCache = createCrossProcessModuleCache(); |
| 60 |
return AST; |
| 61 |
} |
| 62 |
#else |
| 63 |
std::unique_ptr<ASTUnit> |
| 64 |
ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, |
| 65 |
IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
| 66 |
CaptureDiagsKind CaptureDiagnostics, |
| 67 |
bool UserFilesAreVolatile) { |
| 68 |
ASTUNIT_COMMON(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); |
| 69 |
AST->ModuleCache = new InMemoryModuleCache; |
| 70 |
return AST; |
| 71 |
} |
| 72 |
#endif |