| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 1 | //===--- CrossTranslationUnit.cpp - -----------------------------*- C++ -*-===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the CrossTranslationUnit interface. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | #include "clang/CrossTU/CrossTranslationUnit.h" |
| 13 | #include "clang/AST/ASTImporter.h" |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/Basic/TargetInfo.h" |
| 16 | #include "clang/CrossTU/CrossTUDiagnostic.h" |
| 17 | #include "clang/Frontend/ASTUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 20 | #include "clang/Index/USRGeneration.h" |
| 21 | #include "llvm/ADT/Triple.h" |
| Gabor Marton | 700a29a | 2018-12-07 11:55:22 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/ManagedStatic.h" |
| 25 | #include "llvm/Support/Path.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include <fstream> |
| 28 | #include <sstream> |
| 29 | |
| 30 | namespace clang { |
| 31 | namespace cross_tu { |
| 32 | |
| 33 | namespace { |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 34 | |
| Gabor Marton | 700a29a | 2018-12-07 11:55:22 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "CrossTranslationUnit" |
| 36 | STATISTIC(NumGetCTUCalled, "The # of getCTUDefinition function called"); |
| 37 | STATISTIC( |
| 38 | NumNotInOtherTU, |
| 39 | "The # of getCTUDefinition called but the function is not in any other TU"); |
| 40 | STATISTIC(NumGetCTUSuccess, |
| 41 | "The # of getCTUDefinition successfully returned the " |
| 42 | "requested function's body"); |
| Gabor Marton | 8ab8a60 | 2019-06-28 08:08:51 +0000 | [diff] [blame] | 43 | STATISTIC(NumUnsupportedNodeFound, "The # of imports when the ASTImporter " |
| 44 | "encountered an unsupported AST Node"); |
| 45 | STATISTIC(NumNameConflicts, "The # of imports when the ASTImporter " |
| 46 | "encountered an ODR error"); |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 47 | STATISTIC(NumTripleMismatch, "The # of triple mismatches"); |
| 48 | STATISTIC(NumLangMismatch, "The # of language mismatches"); |
| Gabor Marton | a006b80 | 2019-02-28 15:24:59 +0000 | [diff] [blame] | 49 | STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches"); |
| Endre Fulop | 0752d12 | 2019-07-08 12:37:10 +0000 | [diff] [blame] | 50 | STATISTIC(NumASTLoadThresholdReached, |
| 51 | "The # of ASTs not loaded because of threshold"); |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 52 | |
| 53 | // Same as Triple's equality operator, but we check a field only if that is |
| 54 | // known in both instances. |
| 55 | bool hasEqualKnownFields(const llvm::Triple &Lhs, const llvm::Triple &Rhs) { |
| 56 | using llvm::Triple; |
| 57 | if (Lhs.getArch() != Triple::UnknownArch && |
| 58 | Rhs.getArch() != Triple::UnknownArch && Lhs.getArch() != Rhs.getArch()) |
| 59 | return false; |
| 60 | if (Lhs.getSubArch() != Triple::NoSubArch && |
| 61 | Rhs.getSubArch() != Triple::NoSubArch && |
| 62 | Lhs.getSubArch() != Rhs.getSubArch()) |
| 63 | return false; |
| 64 | if (Lhs.getVendor() != Triple::UnknownVendor && |
| 65 | Rhs.getVendor() != Triple::UnknownVendor && |
| 66 | Lhs.getVendor() != Rhs.getVendor()) |
| 67 | return false; |
| 68 | if (!Lhs.isOSUnknown() && !Rhs.isOSUnknown() && |
| 69 | Lhs.getOS() != Rhs.getOS()) |
| 70 | return false; |
| 71 | if (Lhs.getEnvironment() != Triple::UnknownEnvironment && |
| 72 | Rhs.getEnvironment() != Triple::UnknownEnvironment && |
| 73 | Lhs.getEnvironment() != Rhs.getEnvironment()) |
| 74 | return false; |
| 75 | if (Lhs.getObjectFormat() != Triple::UnknownObjectFormat && |
| 76 | Rhs.getObjectFormat() != Triple::UnknownObjectFormat && |
| 77 | Lhs.getObjectFormat() != Rhs.getObjectFormat()) |
| 78 | return false; |
| 79 | return true; |
| 80 | } |
| Gabor Marton | 700a29a | 2018-12-07 11:55:22 +0000 | [diff] [blame] | 81 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 82 | // FIXME: This class is will be removed after the transition to llvm::Error. |
| 83 | class IndexErrorCategory : public std::error_category { |
| 84 | public: |
| 85 | const char *name() const noexcept override { return "clang.index"; } |
| 86 | |
| 87 | std::string message(int Condition) const override { |
| 88 | switch (static_cast<index_error_code>(Condition)) { |
| 89 | case index_error_code::unspecified: |
| 90 | return "An unknown error has occurred."; |
| 91 | case index_error_code::missing_index_file: |
| 92 | return "The index file is missing."; |
| 93 | case index_error_code::invalid_index_format: |
| 94 | return "Invalid index file format."; |
| 95 | case index_error_code::multiple_definitions: |
| 96 | return "Multiple definitions in the index file."; |
| 97 | case index_error_code::missing_definition: |
| 98 | return "Missing definition from the index file."; |
| 99 | case index_error_code::failed_import: |
| 100 | return "Failed to import the definition."; |
| 101 | case index_error_code::failed_to_get_external_ast: |
| 102 | return "Failed to load external AST source."; |
| 103 | case index_error_code::failed_to_generate_usr: |
| 104 | return "Failed to generate USR."; |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 105 | case index_error_code::triple_mismatch: |
| 106 | return "Triple mismatch"; |
| 107 | case index_error_code::lang_mismatch: |
| 108 | return "Language mismatch"; |
| Gabor Marton | a006b80 | 2019-02-28 15:24:59 +0000 | [diff] [blame] | 109 | case index_error_code::lang_dialect_mismatch: |
| 110 | return "Language dialect mismatch"; |
| Endre Fulop | 0752d12 | 2019-07-08 12:37:10 +0000 | [diff] [blame] | 111 | case index_error_code::load_threshold_reached: |
| 112 | return "Load threshold reached"; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 113 | } |
| 114 | llvm_unreachable("Unrecognized index_error_code."); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | static llvm::ManagedStatic<IndexErrorCategory> Category; |
| 119 | } // end anonymous namespace |
| 120 | |
| 121 | char IndexError::ID; |
| 122 | |
| 123 | void IndexError::log(raw_ostream &OS) const { |
| 124 | OS << Category->message(static_cast<int>(Code)) << '\n'; |
| 125 | } |
| 126 | |
| 127 | std::error_code IndexError::convertToErrorCode() const { |
| 128 | return std::error_code(static_cast<int>(Code), *Category); |
| 129 | } |
| 130 | |
| 131 | llvm::Expected<llvm::StringMap<std::string>> |
| 132 | parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir) { |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 133 | std::ifstream ExternalMapFile(IndexPath); |
| 134 | if (!ExternalMapFile) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 135 | return llvm::make_error<IndexError>(index_error_code::missing_index_file, |
| 136 | IndexPath.str()); |
| 137 | |
| 138 | llvm::StringMap<std::string> Result; |
| 139 | std::string Line; |
| 140 | unsigned LineNo = 1; |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 141 | while (std::getline(ExternalMapFile, Line)) { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 142 | const size_t Pos = Line.find(" "); |
| 143 | if (Pos > 0 && Pos != std::string::npos) { |
| 144 | StringRef LineRef{Line}; |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 145 | StringRef LookupName = LineRef.substr(0, Pos); |
| 146 | if (Result.count(LookupName)) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 147 | return llvm::make_error<IndexError>( |
| 148 | index_error_code::multiple_definitions, IndexPath.str(), LineNo); |
| 149 | StringRef FileName = LineRef.substr(Pos + 1); |
| 150 | SmallString<256> FilePath = CrossTUDir; |
| Gabor Horvath | 724beac | 2017-10-27 12:53:37 +0000 | [diff] [blame] | 151 | llvm::sys::path::append(FilePath, FileName); |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 152 | Result[LookupName] = FilePath.str().str(); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 153 | } else |
| 154 | return llvm::make_error<IndexError>( |
| 155 | index_error_code::invalid_index_format, IndexPath.str(), LineNo); |
| 156 | LineNo++; |
| 157 | } |
| 158 | return Result; |
| 159 | } |
| 160 | |
| 161 | std::string |
| 162 | createCrossTUIndexString(const llvm::StringMap<std::string> &Index) { |
| 163 | std::ostringstream Result; |
| 164 | for (const auto &E : Index) |
| 165 | Result << E.getKey().str() << " " << E.getValue() << '\n'; |
| 166 | return Result.str(); |
| 167 | } |
| 168 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 169 | bool containsConst(const VarDecl *VD, const ASTContext &ACtx) { |
| 170 | CanQualType CT = ACtx.getCanonicalType(VD->getType()); |
| 171 | if (!CT.isConstQualified()) { |
| 172 | const RecordType *RTy = CT->getAs<RecordType>(); |
| 173 | if (!RTy || !RTy->hasConstFields()) |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD) { |
| 180 | return D->hasBody(DefD); |
| 181 | } |
| 182 | static bool hasBodyOrInit(const VarDecl *D, const VarDecl *&DefD) { |
| 183 | return D->getAnyInitializer(DefD); |
| 184 | } |
| 185 | template <typename T> static bool hasBodyOrInit(const T *D) { |
| 186 | const T *Unused; |
| 187 | return hasBodyOrInit(D, Unused); |
| 188 | } |
| 189 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 190 | CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI) |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 191 | : Context(CI.getASTContext()), ASTStorage(CI) {} |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 192 | |
| 193 | CrossTranslationUnitContext::~CrossTranslationUnitContext() {} |
| 194 | |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 195 | llvm::Optional<std::string> |
| 196 | CrossTranslationUnitContext::getLookupName(const NamedDecl *ND) { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 197 | SmallString<128> DeclUSR; |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 198 | bool Ret = index::generateUSRForDecl(ND, DeclUSR); |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 199 | if (Ret) |
| 200 | return {}; |
| 201 | return std::string(DeclUSR.str()); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 204 | /// Recursively visits the decls of a DeclContext, and returns one with the |
| 205 | /// given USR. |
| 206 | template <typename T> |
| 207 | const T * |
| 208 | CrossTranslationUnitContext::findDefInDeclContext(const DeclContext *DC, |
| 209 | StringRef LookupName) { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 210 | assert(DC && "Declaration Context must not be null"); |
| 211 | for (const Decl *D : DC->decls()) { |
| 212 | const auto *SubDC = dyn_cast<DeclContext>(D); |
| 213 | if (SubDC) |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 214 | if (const auto *ND = findDefInDeclContext<T>(SubDC, LookupName)) |
| 215 | return ND; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 216 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 217 | const auto *ND = dyn_cast<T>(D); |
| 218 | const T *ResultDecl; |
| 219 | if (!ND || !hasBodyOrInit(ND, ResultDecl)) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 220 | continue; |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 221 | llvm::Optional<std::string> ResultLookupName = getLookupName(ResultDecl); |
| 222 | if (!ResultLookupName || *ResultLookupName != LookupName) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 223 | continue; |
| 224 | return ResultDecl; |
| 225 | } |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 229 | template <typename T> |
| 230 | llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl( |
| 231 | const T *D, StringRef CrossTUDir, StringRef IndexName, |
| 232 | bool DisplayCTUProgress) { |
| 233 | assert(D && "D is missing, bad call to this function!"); |
| 234 | assert(!hasBodyOrInit(D) && |
| 235 | "D has a body or init in current translation unit!"); |
| Gabor Marton | 700a29a | 2018-12-07 11:55:22 +0000 | [diff] [blame] | 236 | ++NumGetCTUCalled; |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 237 | const llvm::Optional<std::string> LookupName = getLookupName(D); |
| 238 | if (!LookupName) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 239 | return llvm::make_error<IndexError>( |
| 240 | index_error_code::failed_to_generate_usr); |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 241 | llvm::Expected<ASTUnit *> ASTUnitOrError = |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 242 | loadExternalAST(*LookupName, CrossTUDir, IndexName, DisplayCTUProgress); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 243 | if (!ASTUnitOrError) |
| 244 | return ASTUnitOrError.takeError(); |
| 245 | ASTUnit *Unit = *ASTUnitOrError; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 246 | assert(&Unit->getFileManager() == |
| 247 | &Unit->getASTContext().getSourceManager().getFileManager()); |
| 248 | |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 249 | const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple(); |
| 250 | const llvm::Triple &TripleFrom = |
| 251 | Unit->getASTContext().getTargetInfo().getTriple(); |
| 252 | // The imported AST had been generated for a different target. |
| 253 | // Some parts of the triple in the loaded ASTContext can be unknown while the |
| 254 | // very same parts in the target ASTContext are known. Thus we check for the |
| 255 | // known parts only. |
| 256 | if (!hasEqualKnownFields(TripleTo, TripleFrom)) { |
| 257 | // TODO: Pass the SourceLocation of the CallExpression for more precise |
| 258 | // diagnostics. |
| 259 | ++NumTripleMismatch; |
| 260 | return llvm::make_error<IndexError>(index_error_code::triple_mismatch, |
| 261 | Unit->getMainFileName(), TripleTo.str(), |
| 262 | TripleFrom.str()); |
| 263 | } |
| 264 | |
| 265 | const auto &LangTo = Context.getLangOpts(); |
| 266 | const auto &LangFrom = Unit->getASTContext().getLangOpts(); |
| Gabor Marton | a006b80 | 2019-02-28 15:24:59 +0000 | [diff] [blame] | 267 | |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 268 | // FIXME: Currenty we do not support CTU across C++ and C and across |
| 269 | // different dialects of C++. |
| 270 | if (LangTo.CPlusPlus != LangFrom.CPlusPlus) { |
| 271 | ++NumLangMismatch; |
| 272 | return llvm::make_error<IndexError>(index_error_code::lang_mismatch); |
| 273 | } |
| 274 | |
| Gabor Marton | a006b80 | 2019-02-28 15:24:59 +0000 | [diff] [blame] | 275 | // If CPP dialects are different then return with error. |
| 276 | // |
| 277 | // Consider this STL code: |
| 278 | // template<typename _Alloc> |
| 279 | // struct __alloc_traits |
| 280 | // #if __cplusplus >= 201103L |
| 281 | // : std::allocator_traits<_Alloc> |
| 282 | // #endif |
| 283 | // { // ... |
| 284 | // }; |
| 285 | // This class template would create ODR errors during merging the two units, |
| 286 | // since in one translation unit the class template has a base class, however |
| 287 | // in the other unit it has none. |
| 288 | if (LangTo.CPlusPlus11 != LangFrom.CPlusPlus11 || |
| 289 | LangTo.CPlusPlus14 != LangFrom.CPlusPlus14 || |
| 290 | LangTo.CPlusPlus17 != LangFrom.CPlusPlus17 || |
| 291 | LangTo.CPlusPlus2a != LangFrom.CPlusPlus2a) { |
| 292 | ++NumLangDialectMismatch; |
| 293 | return llvm::make_error<IndexError>( |
| 294 | index_error_code::lang_dialect_mismatch); |
| 295 | } |
| 296 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 297 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
| Balazs Keri | 4e79097 | 2019-08-06 12:10:16 +0000 | [diff] [blame] | 298 | if (const T *ResultDecl = findDefInDeclContext<T>(TU, *LookupName)) |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 299 | return importDefinition(ResultDecl, Unit); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 300 | return llvm::make_error<IndexError>(index_error_code::failed_import); |
| 301 | } |
| 302 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 303 | llvm::Expected<const FunctionDecl *> |
| 304 | CrossTranslationUnitContext::getCrossTUDefinition(const FunctionDecl *FD, |
| 305 | StringRef CrossTUDir, |
| 306 | StringRef IndexName, |
| 307 | bool DisplayCTUProgress) { |
| 308 | return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName, |
| 309 | DisplayCTUProgress); |
| 310 | } |
| 311 | |
| 312 | llvm::Expected<const VarDecl *> |
| 313 | CrossTranslationUnitContext::getCrossTUDefinition(const VarDecl *VD, |
| 314 | StringRef CrossTUDir, |
| 315 | StringRef IndexName, |
| 316 | bool DisplayCTUProgress) { |
| 317 | return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName, |
| 318 | DisplayCTUProgress); |
| 319 | } |
| 320 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 321 | void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) { |
| 322 | switch (IE.getCode()) { |
| 323 | case index_error_code::missing_index_file: |
| Richard Trieu | 0f25c74 | 2018-12-14 03:35:10 +0000 | [diff] [blame] | 324 | Context.getDiagnostics().Report(diag::err_ctu_error_opening) |
| 325 | << IE.getFileName(); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 326 | break; |
| 327 | case index_error_code::invalid_index_format: |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 328 | Context.getDiagnostics().Report(diag::err_extdefmap_parsing) |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 329 | << IE.getFileName() << IE.getLineNum(); |
| Simon Pilgrim | 554ab53 | 2017-09-24 15:17:46 +0000 | [diff] [blame] | 330 | break; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 331 | case index_error_code::multiple_definitions: |
| 332 | Context.getDiagnostics().Report(diag::err_multiple_def_index) |
| 333 | << IE.getLineNum(); |
| 334 | break; |
| Gabor Marton | 32aff2e | 2018-12-07 16:32:43 +0000 | [diff] [blame] | 335 | case index_error_code::triple_mismatch: |
| 336 | Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple) |
| 337 | << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName(); |
| 338 | break; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 339 | default: |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 344 | CrossTranslationUnitContext::ASTFileLoader::ASTFileLoader( |
| 345 | const CompilerInstance &CI) |
| 346 | : CI(CI) {} |
| 347 | |
| 348 | std::unique_ptr<ASTUnit> |
| 349 | CrossTranslationUnitContext::ASTFileLoader::operator()(StringRef ASTFilePath) { |
| 350 | // Load AST from ast-dump. |
| 351 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 352 | TextDiagnosticPrinter *DiagClient = |
| 353 | new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); |
| 354 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 355 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 356 | new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient)); |
| 357 | |
| 358 | return ASTUnit::LoadFromASTFile( |
| 359 | ASTFilePath, CI.getPCHContainerOperations()->getRawReader(), |
| 360 | ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts()); |
| 361 | } |
| 362 | |
| 363 | CrossTranslationUnitContext::ASTUnitStorage::ASTUnitStorage( |
| 364 | const CompilerInstance &CI) |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 365 | : FileAccessor(CI), LoadGuard(const_cast<CompilerInstance &>(CI) |
| 366 | .getAnalyzerOpts() |
| 367 | ->CTUImportThreshold) {} |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 368 | |
| 369 | llvm::Expected<ASTUnit *> |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 370 | CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFile( |
| 371 | StringRef FileName, bool DisplayCTUProgress) { |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 372 | // Try the cache first. |
| 373 | auto ASTCacheEntry = FileASTUnitMap.find(FileName); |
| 374 | if (ASTCacheEntry == FileASTUnitMap.end()) { |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 375 | |
| 376 | // Do not load if the limit is reached. |
| 377 | if (!LoadGuard) { |
| 378 | ++NumASTLoadThresholdReached; |
| 379 | return llvm::make_error<IndexError>( |
| 380 | index_error_code::load_threshold_reached); |
| 381 | } |
| 382 | |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 383 | // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName. |
| 384 | std::unique_ptr<ASTUnit> LoadedUnit = FileAccessor(FileName); |
| 385 | |
| 386 | // Need the raw pointer and the unique_ptr as well. |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 387 | ASTUnit *Unit = LoadedUnit.get(); |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 388 | |
| 389 | // Update the cache. |
| 390 | FileASTUnitMap[FileName] = std::move(LoadedUnit); |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 391 | |
| 392 | LoadGuard.indicateLoadSuccess(); |
| 393 | |
| 394 | if (DisplayCTUProgress) |
| 395 | llvm::errs() << "CTU loaded AST file: " << FileName << "\n"; |
| 396 | |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 397 | return Unit; |
| 398 | |
| 399 | } else { |
| 400 | // Found in the cache. |
| 401 | return ASTCacheEntry->second.get(); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | llvm::Expected<ASTUnit *> |
| 406 | CrossTranslationUnitContext::ASTUnitStorage::getASTUnitForFunction( |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 407 | StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName, |
| 408 | bool DisplayCTUProgress) { |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 409 | // Try the cache first. |
| 410 | auto ASTCacheEntry = NameASTUnitMap.find(FunctionName); |
| 411 | if (ASTCacheEntry == NameASTUnitMap.end()) { |
| 412 | // Load the ASTUnit from the pre-dumped AST file specified by ASTFileName. |
| 413 | |
| 414 | // Ensure that the Index is loaded, as we need to search in it. |
| 415 | if (llvm::Error IndexLoadError = |
| 416 | ensureCTUIndexLoaded(CrossTUDir, IndexName)) |
| 417 | return std::move(IndexLoadError); |
| 418 | |
| 419 | // Check if there is and entry in the index for the function. |
| 420 | if (!NameFileMap.count(FunctionName)) { |
| 421 | ++NumNotInOtherTU; |
| 422 | return llvm::make_error<IndexError>(index_error_code::missing_definition); |
| 423 | } |
| 424 | |
| 425 | // Search in the index for the filename where the definition of FuncitonName |
| 426 | // resides. |
| 427 | if (llvm::Expected<ASTUnit *> FoundForFile = |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 428 | getASTUnitForFile(NameFileMap[FunctionName], DisplayCTUProgress)) { |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 429 | |
| 430 | // Update the cache. |
| 431 | NameASTUnitMap[FunctionName] = *FoundForFile; |
| 432 | return *FoundForFile; |
| 433 | |
| 434 | } else { |
| 435 | return FoundForFile.takeError(); |
| 436 | } |
| 437 | } else { |
| 438 | // Found in the cache. |
| 439 | return ASTCacheEntry->second; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | llvm::Expected<std::string> |
| 444 | CrossTranslationUnitContext::ASTUnitStorage::getFileForFunction( |
| 445 | StringRef FunctionName, StringRef CrossTUDir, StringRef IndexName) { |
| 446 | if (llvm::Error IndexLoadError = ensureCTUIndexLoaded(CrossTUDir, IndexName)) |
| 447 | return std::move(IndexLoadError); |
| 448 | return NameFileMap[FunctionName]; |
| 449 | } |
| 450 | |
| 451 | llvm::Error CrossTranslationUnitContext::ASTUnitStorage::ensureCTUIndexLoaded( |
| 452 | StringRef CrossTUDir, StringRef IndexName) { |
| 453 | // Dont initialize if the map is filled. |
| 454 | if (!NameFileMap.empty()) |
| 455 | return llvm::Error::success(); |
| 456 | |
| 457 | // Get the absolute path to the index file. |
| 458 | SmallString<256> IndexFile = CrossTUDir; |
| 459 | if (llvm::sys::path::is_absolute(IndexName)) |
| 460 | IndexFile = IndexName; |
| 461 | else |
| 462 | llvm::sys::path::append(IndexFile, IndexName); |
| 463 | |
| 464 | if (auto IndexMapping = parseCrossTUIndex(IndexFile, CrossTUDir)) { |
| 465 | // Initialize member map. |
| 466 | NameFileMap = *IndexMapping; |
| 467 | return llvm::Error::success(); |
| 468 | } else { |
| 469 | // Error while parsing CrossTU index file. |
| 470 | return IndexMapping.takeError(); |
| 471 | }; |
| 472 | } |
| 473 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 474 | llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST( |
| Gabor Marton | 9419eb4 | 2018-12-07 14:56:02 +0000 | [diff] [blame] | 475 | StringRef LookupName, StringRef CrossTUDir, StringRef IndexName, |
| 476 | bool DisplayCTUProgress) { |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 477 | // FIXME: The current implementation only supports loading decls with |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 478 | // a lookup name from a single translation unit. If multiple |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 479 | // translation units contains decls with the same lookup name an |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 480 | // error will be returned. |
| Endre Fulop | 0752d12 | 2019-07-08 12:37:10 +0000 | [diff] [blame] | 481 | |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 482 | // Try to get the value from the heavily cached storage. |
| Balazs Keri | 4b9d200 | 2019-08-12 07:15:29 +0000 | [diff] [blame] | 483 | llvm::Expected<ASTUnit *> Unit = ASTStorage.getASTUnitForFunction( |
| 484 | LookupName, CrossTUDir, IndexName, DisplayCTUProgress); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 485 | |
| Gabor Marton | 3081690 | 2019-01-07 14:05:19 +0000 | [diff] [blame] | 486 | if (!Unit) |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 487 | return Unit.takeError(); |
| 488 | |
| 489 | // Check whether the backing pointer of the Expected is a nullptr. |
| 490 | if (!*Unit) |
| Gabor Marton | 3081690 | 2019-01-07 14:05:19 +0000 | [diff] [blame] | 491 | return llvm::make_error<IndexError>( |
| 492 | index_error_code::failed_to_get_external_ast); |
| Endre Fulop | 0492fd4 | 2019-08-05 11:06:41 +0000 | [diff] [blame] | 493 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 494 | return Unit; |
| 495 | } |
| 496 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 497 | template <typename T> |
| 498 | llvm::Expected<const T *> |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 499 | CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) { |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 500 | assert(hasBodyOrInit(D) && "Decls to be imported should have body or init."); |
| Gabor Marton | b7f30dd | 2018-12-07 12:21:43 +0000 | [diff] [blame] | 501 | |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 502 | assert(&D->getASTContext() == &Unit->getASTContext() && |
| 503 | "ASTContext of Decl and the unit should match."); |
| 504 | ASTImporter &Importer = getOrCreateASTImporter(Unit); |
| 505 | |
| Gabor Marton | 5ac6d49 | 2019-05-15 10:29:48 +0000 | [diff] [blame] | 506 | auto ToDeclOrError = Importer.Import(D); |
| Balazs Keri | a1f6b10 | 2019-04-08 13:59:15 +0000 | [diff] [blame] | 507 | if (!ToDeclOrError) { |
| 508 | handleAllErrors(ToDeclOrError.takeError(), |
| 509 | [&](const ImportError &IE) { |
| 510 | switch (IE.Error) { |
| 511 | case ImportError::NameConflict: |
| Gabor Marton | 8ab8a60 | 2019-06-28 08:08:51 +0000 | [diff] [blame] | 512 | ++NumNameConflicts; |
| Balazs Keri | a1f6b10 | 2019-04-08 13:59:15 +0000 | [diff] [blame] | 513 | break; |
| 514 | case ImportError::UnsupportedConstruct: |
| Gabor Marton | 8ab8a60 | 2019-06-28 08:08:51 +0000 | [diff] [blame] | 515 | ++NumUnsupportedNodeFound; |
| Balazs Keri | a1f6b10 | 2019-04-08 13:59:15 +0000 | [diff] [blame] | 516 | break; |
| 517 | case ImportError::Unknown: |
| 518 | llvm_unreachable("Unknown import error happened."); |
| 519 | break; |
| 520 | } |
| 521 | }); |
| Gabor Marton | b87251d | 2018-12-07 16:05:58 +0000 | [diff] [blame] | 522 | return llvm::make_error<IndexError>(index_error_code::failed_import); |
| Balazs Keri | a1f6b10 | 2019-04-08 13:59:15 +0000 | [diff] [blame] | 523 | } |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 524 | auto *ToDecl = cast<T>(*ToDeclOrError); |
| 525 | assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init."); |
| Gabor Marton | 700a29a | 2018-12-07 11:55:22 +0000 | [diff] [blame] | 526 | ++NumGetCTUSuccess; |
| Balazs Keri | a1f6b10 | 2019-04-08 13:59:15 +0000 | [diff] [blame] | 527 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 528 | return ToDecl; |
| 529 | } |
| 530 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 531 | llvm::Expected<const FunctionDecl *> |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 532 | CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD, |
| 533 | ASTUnit *Unit) { |
| 534 | return importDefinitionImpl(FD, Unit); |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | llvm::Expected<const VarDecl *> |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 538 | CrossTranslationUnitContext::importDefinition(const VarDecl *VD, |
| 539 | ASTUnit *Unit) { |
| 540 | return importDefinitionImpl(VD, Unit); |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| Gabor Marton | 2afbfb6 | 2019-07-01 15:37:07 +0000 | [diff] [blame] | 543 | void CrossTranslationUnitContext::lazyInitImporterSharedSt( |
| Gabor Marton | 54058b5 | 2018-12-17 13:53:12 +0000 | [diff] [blame] | 544 | TranslationUnitDecl *ToTU) { |
| Gabor Marton | 2afbfb6 | 2019-07-01 15:37:07 +0000 | [diff] [blame] | 545 | if (!ImporterSharedSt) |
| 546 | ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU); |
| Gabor Marton | 54058b5 | 2018-12-17 13:53:12 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 549 | ASTImporter & |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 550 | CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) { |
| 551 | ASTContext &From = Unit->getASTContext(); |
| 552 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 553 | auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl()); |
| 554 | if (I != ASTUnitImporterMap.end()) |
| 555 | return *I->second; |
| Gabor Marton | 2afbfb6 | 2019-07-01 15:37:07 +0000 | [diff] [blame] | 556 | lazyInitImporterSharedSt(Context.getTranslationUnitDecl()); |
| Ilya Biryukov | abc744d | 2019-07-18 15:43:26 +0000 | [diff] [blame] | 557 | ASTImporter *NewImporter = new ASTImporter( |
| 558 | Context, Context.getSourceManager().getFileManager(), From, |
| 559 | From.getSourceManager().getFileManager(), false, ImporterSharedSt); |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 560 | NewImporter->setFileIDImportHandler([this, Unit](FileID ToID, FileID FromID) { |
| 561 | assert(ImportedFileIDs.find(ToID) == ImportedFileIDs.end() && |
| 562 | "FileID already imported, should not happen."); |
| 563 | ImportedFileIDs[ToID] = std::make_pair(FromID, Unit); |
| 564 | }); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 565 | ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter); |
| 566 | return *NewImporter; |
| 567 | } |
| 568 | |
| Balazs Keri | d22f877 | 2019-07-24 10:16:37 +0000 | [diff] [blame] | 569 | llvm::Optional<std::pair<SourceLocation, ASTUnit *>> |
| 570 | CrossTranslationUnitContext::getImportedFromSourceLocation( |
| 571 | const clang::SourceLocation &ToLoc) const { |
| 572 | const SourceManager &SM = Context.getSourceManager(); |
| 573 | auto DecToLoc = SM.getDecomposedLoc(ToLoc); |
| 574 | |
| 575 | auto I = ImportedFileIDs.find(DecToLoc.first); |
| 576 | if (I == ImportedFileIDs.end()) |
| 577 | return {}; |
| 578 | |
| 579 | FileID FromID = I->second.first; |
| 580 | clang::ASTUnit *Unit = I->second.second; |
| 581 | SourceLocation FromLoc = |
| 582 | Unit->getSourceManager().getComposedLoc(FromID, DecToLoc.second); |
| 583 | |
| 584 | return std::make_pair(FromLoc, Unit); |
| 585 | } |
| 586 | |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 587 | } // namespace cross_tu |
| 588 | } // namespace clang |