| 1 |
//===- ClangCompat.hh - Compatibility layer for multiple clang versions *- 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 |
/// This file provides a compatibility layer to clang-extract for it to compile |
| 11 |
/// with multiple versions of clang and LLVM. |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
#pragma once |
| 16 |
|
| 17 |
#include <clang/Tooling/Tooling.h> |
| 18 |
#include <clang/Basic/Version.h> |
| 19 |
#include "clang/Frontend/CompilerInstance.h" |
| 20 |
|
| 21 |
/* Starting from LLVM-18, the method FileEntry::getName() got deprecated. |
| 22 |
* This makes clang warns about the function being deprecated while it is |
| 23 |
* completely useful as a way to keep compatibility with older versions of |
| 24 |
* LLVM, hence we acknowledge the warning here and disable it for now. |
| 25 |
*/ |
| 26 |
|
| 27 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 28 |
|
| 29 |
namespace ClangCompat |
| 30 |
{ |
| 31 |
#if CLANG_VERSION_MAJOR >= 16 |
| 32 |
/** Some functions changed its signature to expect std::nullopt when no option is given.*/ |
| 33 |
# define ClangCompat_None std::nullopt |
| 34 |
/** Clang do not provide getPointer() for tokens anymore. */ |
| 35 |
# define ClangCompat_GetTokenPtr(token) (token).has_value() ? &(token).value() : nullptr |
| 36 |
#else |
| 37 |
# define ClangCompat_None clang::None |
| 38 |
# define ClangCompat_GetTokenPtr(token) (token).getPointer() |
| 39 |
#endif |
| 40 |
|
| 41 |
/** Starting from LLVM-21, ASTUnit::create requires one more parameter. */ |
| 42 |
#if CLANG_VERSION_MAJOR >= 21 |
| 43 |
# define ClangCompat_ASTUP(CI, DO, D, CP, UFAV) CI, DO, D, CP, UFAV |
| 44 |
# define ClangCompat_ASTULFCIAP(CI, PCH, DO, D, A, U, P, RFP, OLC, CD, PPANP, CCCR, UFAV, EAST) \ |
| 45 |
CI, PCH, DO, D, A, U, P, RFP, OLC, CD, PPANP, CCCR, UFAV, EAST |
| 46 |
#else |
| 47 |
# define ClangCompat_ASTUP(CI, DO, D, CP, UFAV) CI, D, CP, UFAV |
| 48 |
# define ClangCompat_ASTULFCIAP(CI, PCH, DO, D, A, U, P, RFP, OLC, CD, PPANP, CCCR, UFAV, EAST) \ |
| 49 |
CI, PCH, D, A, U, P, RFP, OLC, CD, PPANP, CCCR, UFAV, EAST |
| 50 |
#endif |
| 51 |
|
| 52 |
static inline const clang::TypedefType *Get_Type_As_TypedefType(const clang::Type *type) |
| 53 |
{ |
| 54 |
#if CLANG_VERSION_MAJOR >= 16 |
| 55 |
/* Clang 16 upwards do not provide getAs<const TypedefType>. */ |
| 56 |
return type->getAs<clang::TypedefType>(); |
| 57 |
#else |
| 58 |
return type->getAs<const clang::TypedefType>(); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
static inline auto |
| 63 |
createInvocationFromCommandLine(ArrayRef<const char *> Args, IntrusiveRefCntPtr<DiagnosticsEngine> Diags=IntrusiveRefCntPtr< DiagnosticsEngine >()) |
| 64 |
{ |
| 65 |
#if CLANG_VERSION_MAJOR >= 15 |
| 66 |
/* Provide an implementation of createInvocationFromCommandLine for newer versions of clang. */ |
| 67 |
clang::CreateInvocationOptions CIOpts; |
| 68 |
CIOpts.Diags = Diags; |
| 69 |
return clang::createInvocation(Args, std::move(CIOpts)); |
| 70 |
#else |
| 71 |
return clang::createInvocationFromCommandLine(Args, Diags); |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
static inline const clang::Type *getTypePtr(const QualType &qtype) |
| 76 |
{ |
| 77 |
#if CLANG_VERSION_MAJOR >= 17 |
| 78 |
/* Starting from clang-17 it crashes if qtype isNull is true. */ |
| 79 |
return qtype.isNull() ? nullptr : qtype.getTypePtr(); |
| 80 |
#else |
| 81 |
return qtype.getTypePtr(); |
| 82 |
#endif |
| 83 |
} |
| 84 |
|
| 85 |
static inline auto Get_Main_Directory_Arr(const SourceManager &sm) |
| 86 |
{ |
| 87 |
/* This function is used to provide a valid object for the function |
| 88 |
`LookupFile`, which do not seems to provide a stable interface between |
| 89 |
versions. */ |
| 90 |
FileID main_file = sm.getMainFileID(); |
| 91 |
OptionalFileEntryRef main_fentry = sm.getFileEntryRefForID(main_file); |
| 92 |
DirectoryEntryRef dir_ref = (*main_fentry).getDir(); |
| 93 |
|
| 94 |
#if CLANG_VERSION_MAJOR >= 18 |
| 95 |
std::array<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 1> A {{ |
| 96 |
{ main_fentry, dir_ref }, |
| 97 |
}}; |
| 98 |
return A; |
| 99 |
#elif CLANG_VERSION_MAJOR == 17 |
| 100 |
const FileEntry *fentry = &(*main_fentry).getFileEntry(); |
| 101 |
|
| 102 |
std::array<std::pair<const FileEntry *, DirectoryEntryRef>, 1> A {{ |
| 103 |
{ fentry, dir_ref }, |
| 104 |
}}; |
| 105 |
return A; |
| 106 |
#else |
| 107 |
const FileEntry *fentry = &(*main_fentry).getFileEntry(); |
| 108 |
const DirectoryEntry *dentry = &dir_ref.getDirEntry(); |
| 109 |
|
| 110 |
std::array<std::pair<const FileEntry *, const DirectoryEntry *>, 1> A {{ |
| 111 |
{ fentry, dentry }, |
| 112 |
}}; |
| 113 |
return A; |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
#if CLANG_VERSION_MAJOR >= 21 |
| 118 |
static inline IntrusiveRefCntPtr<DiagnosticsEngine> createDiagnostics( |
| 119 |
llvm::vfs::FileSystem &VFS, |
| 120 |
std::shared_ptr<DiagnosticOptions> Opts) |
| 121 |
{ |
| 122 |
return CompilerInstance::createDiagnostics(VFS, *Opts); |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
static inline IntrusiveRefCntPtr<DiagnosticsEngine> createDiagnostics( |
| 127 |
llvm::vfs::FileSystem &VFS, |
| 128 |
DiagnosticOptions *Opts) |
| 129 |
{ |
| 130 |
#if CLANG_VERSION_MAJOR >= 21 |
| 131 |
return CompilerInstance::createDiagnostics(VFS, *Opts); |
| 132 |
#elif CLANG_VERSION_MAJOR == 20 |
| 133 |
return CompilerInstance::createDiagnostics(VFS, Opts); |
| 134 |
#else |
| 135 |
return CompilerInstance::createDiagnostics(Opts); |
| 136 |
#endif |
| 137 |
} |
| 138 |
|
| 139 |
#if CLANG_VERSION_MAJOR >= 21 |
| 140 |
static inline std::shared_ptr<DiagnosticOptions> createDiagnosticOptions(void) |
| 141 |
{ |
| 142 |
return std::make_shared<DiagnosticOptions>(); |
| 143 |
} |
| 144 |
#else |
| 145 |
static inline DiagnosticOptions *createDiagnosticOptions(void) |
| 146 |
{ |
| 147 |
return new DiagnosticOptions(); |
| 148 |
} |
| 149 |
#endif |
| 150 |
} |