2017-09-22 19:11:01 +08:00
|
|
|
//===--- CrossTranslationUnit.cpp - -----------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-22 19:11:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CrossTranslationUnit interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/CrossTU/CrossTranslationUnit.h"
|
|
|
|
#include "clang/AST/ASTImporter.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/CrossTU/CrossTUDiagnostic.h"
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Index/USRGeneration.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2018-12-07 19:55:22 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-09-22 19:11:01 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace cross_tu {
|
|
|
|
|
|
|
|
namespace {
|
2018-12-08 00:32:43 +08:00
|
|
|
|
2018-12-07 19:55:22 +08:00
|
|
|
#define DEBUG_TYPE "CrossTranslationUnit"
|
|
|
|
STATISTIC(NumGetCTUCalled, "The # of getCTUDefinition function called");
|
|
|
|
STATISTIC(
|
|
|
|
NumNotInOtherTU,
|
|
|
|
"The # of getCTUDefinition called but the function is not in any other TU");
|
|
|
|
STATISTIC(NumGetCTUSuccess,
|
|
|
|
"The # of getCTUDefinition successfully returned the "
|
|
|
|
"requested function's body");
|
2019-06-28 16:08:51 +08:00
|
|
|
STATISTIC(NumUnsupportedNodeFound, "The # of imports when the ASTImporter "
|
|
|
|
"encountered an unsupported AST Node");
|
|
|
|
STATISTIC(NumNameConflicts, "The # of imports when the ASTImporter "
|
|
|
|
"encountered an ODR error");
|
2018-12-08 00:32:43 +08:00
|
|
|
STATISTIC(NumTripleMismatch, "The # of triple mismatches");
|
|
|
|
STATISTIC(NumLangMismatch, "The # of language mismatches");
|
2019-02-28 23:24:59 +08:00
|
|
|
STATISTIC(NumLangDialectMismatch, "The # of language dialect mismatches");
|
2019-07-08 20:37:10 +08:00
|
|
|
STATISTIC(NumASTLoadThresholdReached,
|
|
|
|
"The # of ASTs not loaded because of threshold");
|
2018-12-08 00:32:43 +08:00
|
|
|
|
|
|
|
// Same as Triple's equality operator, but we check a field only if that is
|
|
|
|
// known in both instances.
|
|
|
|
bool hasEqualKnownFields(const llvm::Triple &Lhs, const llvm::Triple &Rhs) {
|
|
|
|
using llvm::Triple;
|
|
|
|
if (Lhs.getArch() != Triple::UnknownArch &&
|
|
|
|
Rhs.getArch() != Triple::UnknownArch && Lhs.getArch() != Rhs.getArch())
|
|
|
|
return false;
|
|
|
|
if (Lhs.getSubArch() != Triple::NoSubArch &&
|
|
|
|
Rhs.getSubArch() != Triple::NoSubArch &&
|
|
|
|
Lhs.getSubArch() != Rhs.getSubArch())
|
|
|
|
return false;
|
|
|
|
if (Lhs.getVendor() != Triple::UnknownVendor &&
|
|
|
|
Rhs.getVendor() != Triple::UnknownVendor &&
|
|
|
|
Lhs.getVendor() != Rhs.getVendor())
|
|
|
|
return false;
|
|
|
|
if (!Lhs.isOSUnknown() && !Rhs.isOSUnknown() &&
|
|
|
|
Lhs.getOS() != Rhs.getOS())
|
|
|
|
return false;
|
|
|
|
if (Lhs.getEnvironment() != Triple::UnknownEnvironment &&
|
|
|
|
Rhs.getEnvironment() != Triple::UnknownEnvironment &&
|
|
|
|
Lhs.getEnvironment() != Rhs.getEnvironment())
|
|
|
|
return false;
|
|
|
|
if (Lhs.getObjectFormat() != Triple::UnknownObjectFormat &&
|
|
|
|
Rhs.getObjectFormat() != Triple::UnknownObjectFormat &&
|
|
|
|
Lhs.getObjectFormat() != Rhs.getObjectFormat())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-07 19:55:22 +08:00
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
// FIXME: This class is will be removed after the transition to llvm::Error.
|
|
|
|
class IndexErrorCategory : public std::error_category {
|
|
|
|
public:
|
|
|
|
const char *name() const noexcept override { return "clang.index"; }
|
|
|
|
|
|
|
|
std::string message(int Condition) const override {
|
|
|
|
switch (static_cast<index_error_code>(Condition)) {
|
|
|
|
case index_error_code::unspecified:
|
|
|
|
return "An unknown error has occurred.";
|
|
|
|
case index_error_code::missing_index_file:
|
|
|
|
return "The index file is missing.";
|
|
|
|
case index_error_code::invalid_index_format:
|
|
|
|
return "Invalid index file format.";
|
|
|
|
case index_error_code::multiple_definitions:
|
|
|
|
return "Multiple definitions in the index file.";
|
|
|
|
case index_error_code::missing_definition:
|
|
|
|
return "Missing definition from the index file.";
|
|
|
|
case index_error_code::failed_import:
|
|
|
|
return "Failed to import the definition.";
|
|
|
|
case index_error_code::failed_to_get_external_ast:
|
|
|
|
return "Failed to load external AST source.";
|
|
|
|
case index_error_code::failed_to_generate_usr:
|
|
|
|
return "Failed to generate USR.";
|
2018-12-08 00:32:43 +08:00
|
|
|
case index_error_code::triple_mismatch:
|
|
|
|
return "Triple mismatch";
|
|
|
|
case index_error_code::lang_mismatch:
|
|
|
|
return "Language mismatch";
|
2019-02-28 23:24:59 +08:00
|
|
|
case index_error_code::lang_dialect_mismatch:
|
|
|
|
return "Language dialect mismatch";
|
2019-07-08 20:37:10 +08:00
|
|
|
case index_error_code::load_threshold_reached:
|
|
|
|
return "Load threshold reached";
|
2017-09-22 19:11:01 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unrecognized index_error_code.");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static llvm::ManagedStatic<IndexErrorCategory> Category;
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char IndexError::ID;
|
|
|
|
|
|
|
|
void IndexError::log(raw_ostream &OS) const {
|
|
|
|
OS << Category->message(static_cast<int>(Code)) << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code IndexError::convertToErrorCode() const {
|
|
|
|
return std::error_code(static_cast<int>(Code), *Category);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<llvm::StringMap<std::string>>
|
|
|
|
parseCrossTUIndex(StringRef IndexPath, StringRef CrossTUDir) {
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
std::ifstream ExternalMapFile(IndexPath);
|
|
|
|
if (!ExternalMapFile)
|
2017-09-22 19:11:01 +08:00
|
|
|
return llvm::make_error<IndexError>(index_error_code::missing_index_file,
|
|
|
|
IndexPath.str());
|
|
|
|
|
|
|
|
llvm::StringMap<std::string> Result;
|
|
|
|
std::string Line;
|
|
|
|
unsigned LineNo = 1;
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
while (std::getline(ExternalMapFile, Line)) {
|
2017-09-22 19:11:01 +08:00
|
|
|
const size_t Pos = Line.find(" ");
|
|
|
|
if (Pos > 0 && Pos != std::string::npos) {
|
|
|
|
StringRef LineRef{Line};
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
StringRef LookupName = LineRef.substr(0, Pos);
|
|
|
|
if (Result.count(LookupName))
|
2017-09-22 19:11:01 +08:00
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::multiple_definitions, IndexPath.str(), LineNo);
|
|
|
|
StringRef FileName = LineRef.substr(Pos + 1);
|
|
|
|
SmallString<256> FilePath = CrossTUDir;
|
2017-10-27 20:53:37 +08:00
|
|
|
llvm::sys::path::append(FilePath, FileName);
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
Result[LookupName] = FilePath.str().str();
|
2017-09-22 19:11:01 +08:00
|
|
|
} else
|
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::invalid_index_format, IndexPath.str(), LineNo);
|
|
|
|
LineNo++;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
createCrossTUIndexString(const llvm::StringMap<std::string> &Index) {
|
|
|
|
std::ostringstream Result;
|
|
|
|
for (const auto &E : Index)
|
|
|
|
Result << E.getKey().str() << " " << E.getValue() << '\n';
|
|
|
|
return Result.str();
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
bool containsConst(const VarDecl *VD, const ASTContext &ACtx) {
|
|
|
|
CanQualType CT = ACtx.getCanonicalType(VD->getType());
|
|
|
|
if (!CT.isConstQualified()) {
|
|
|
|
const RecordType *RTy = CT->getAs<RecordType>();
|
|
|
|
if (!RTy || !RTy->hasConstFields())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hasBodyOrInit(const FunctionDecl *D, const FunctionDecl *&DefD) {
|
|
|
|
return D->hasBody(DefD);
|
|
|
|
}
|
|
|
|
static bool hasBodyOrInit(const VarDecl *D, const VarDecl *&DefD) {
|
|
|
|
return D->getAnyInitializer(DefD);
|
|
|
|
}
|
|
|
|
template <typename T> static bool hasBodyOrInit(const T *D) {
|
|
|
|
const T *Unused;
|
|
|
|
return hasBodyOrInit(D, Unused);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
CrossTranslationUnitContext::CrossTranslationUnitContext(CompilerInstance &CI)
|
2019-07-08 20:37:10 +08:00
|
|
|
: CI(CI), Context(CI.getASTContext()),
|
|
|
|
CTULoadThreshold(CI.getAnalyzerOpts()->CTUImportThreshold) {}
|
2017-09-22 19:11:01 +08:00
|
|
|
|
|
|
|
CrossTranslationUnitContext::~CrossTranslationUnitContext() {}
|
|
|
|
|
|
|
|
std::string CrossTranslationUnitContext::getLookupName(const NamedDecl *ND) {
|
|
|
|
SmallString<128> DeclUSR;
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
bool Ret = index::generateUSRForDecl(ND, DeclUSR);
|
|
|
|
(void)Ret;
|
2017-09-22 19:11:01 +08:00
|
|
|
assert(!Ret && "Unable to generate USR");
|
|
|
|
return DeclUSR.str();
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
/// Recursively visits the decls of a DeclContext, and returns one with the
|
|
|
|
/// given USR.
|
|
|
|
template <typename T>
|
|
|
|
const T *
|
|
|
|
CrossTranslationUnitContext::findDefInDeclContext(const DeclContext *DC,
|
|
|
|
StringRef LookupName) {
|
2017-09-22 19:11:01 +08:00
|
|
|
assert(DC && "Declaration Context must not be null");
|
|
|
|
for (const Decl *D : DC->decls()) {
|
|
|
|
const auto *SubDC = dyn_cast<DeclContext>(D);
|
|
|
|
if (SubDC)
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
if (const auto *ND = findDefInDeclContext<T>(SubDC, LookupName))
|
|
|
|
return ND;
|
2017-09-22 19:11:01 +08:00
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
const auto *ND = dyn_cast<T>(D);
|
|
|
|
const T *ResultDecl;
|
|
|
|
if (!ND || !hasBodyOrInit(ND, ResultDecl))
|
2017-09-22 19:11:01 +08:00
|
|
|
continue;
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
if (getLookupName(ResultDecl) != LookupName)
|
2017-09-22 19:11:01 +08:00
|
|
|
continue;
|
|
|
|
return ResultDecl;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
template <typename T>
|
|
|
|
llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl(
|
|
|
|
const T *D, StringRef CrossTUDir, StringRef IndexName,
|
|
|
|
bool DisplayCTUProgress) {
|
|
|
|
assert(D && "D is missing, bad call to this function!");
|
|
|
|
assert(!hasBodyOrInit(D) &&
|
|
|
|
"D has a body or init in current translation unit!");
|
2018-12-07 19:55:22 +08:00
|
|
|
++NumGetCTUCalled;
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
const std::string LookupName = getLookupName(D);
|
|
|
|
if (LookupName.empty())
|
2017-09-22 19:11:01 +08:00
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::failed_to_generate_usr);
|
2019-07-08 20:37:10 +08:00
|
|
|
llvm::Expected<ASTUnit *> ASTUnitOrError = loadExternalAST(
|
|
|
|
LookupName, CrossTUDir, IndexName, DisplayCTUProgress);
|
2017-09-22 19:11:01 +08:00
|
|
|
if (!ASTUnitOrError)
|
|
|
|
return ASTUnitOrError.takeError();
|
|
|
|
ASTUnit *Unit = *ASTUnitOrError;
|
|
|
|
assert(&Unit->getFileManager() ==
|
|
|
|
&Unit->getASTContext().getSourceManager().getFileManager());
|
|
|
|
|
2018-12-08 00:32:43 +08:00
|
|
|
const llvm::Triple &TripleTo = Context.getTargetInfo().getTriple();
|
|
|
|
const llvm::Triple &TripleFrom =
|
|
|
|
Unit->getASTContext().getTargetInfo().getTriple();
|
|
|
|
// The imported AST had been generated for a different target.
|
|
|
|
// Some parts of the triple in the loaded ASTContext can be unknown while the
|
|
|
|
// very same parts in the target ASTContext are known. Thus we check for the
|
|
|
|
// known parts only.
|
|
|
|
if (!hasEqualKnownFields(TripleTo, TripleFrom)) {
|
|
|
|
// TODO: Pass the SourceLocation of the CallExpression for more precise
|
|
|
|
// diagnostics.
|
|
|
|
++NumTripleMismatch;
|
|
|
|
return llvm::make_error<IndexError>(index_error_code::triple_mismatch,
|
|
|
|
Unit->getMainFileName(), TripleTo.str(),
|
|
|
|
TripleFrom.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &LangTo = Context.getLangOpts();
|
|
|
|
const auto &LangFrom = Unit->getASTContext().getLangOpts();
|
2019-02-28 23:24:59 +08:00
|
|
|
|
2018-12-08 00:32:43 +08:00
|
|
|
// FIXME: Currenty we do not support CTU across C++ and C and across
|
|
|
|
// different dialects of C++.
|
|
|
|
if (LangTo.CPlusPlus != LangFrom.CPlusPlus) {
|
|
|
|
++NumLangMismatch;
|
|
|
|
return llvm::make_error<IndexError>(index_error_code::lang_mismatch);
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:24:59 +08:00
|
|
|
// If CPP dialects are different then return with error.
|
|
|
|
//
|
|
|
|
// Consider this STL code:
|
|
|
|
// template<typename _Alloc>
|
|
|
|
// struct __alloc_traits
|
|
|
|
// #if __cplusplus >= 201103L
|
|
|
|
// : std::allocator_traits<_Alloc>
|
|
|
|
// #endif
|
|
|
|
// { // ...
|
|
|
|
// };
|
|
|
|
// This class template would create ODR errors during merging the two units,
|
|
|
|
// since in one translation unit the class template has a base class, however
|
|
|
|
// in the other unit it has none.
|
|
|
|
if (LangTo.CPlusPlus11 != LangFrom.CPlusPlus11 ||
|
|
|
|
LangTo.CPlusPlus14 != LangFrom.CPlusPlus14 ||
|
|
|
|
LangTo.CPlusPlus17 != LangFrom.CPlusPlus17 ||
|
|
|
|
LangTo.CPlusPlus2a != LangFrom.CPlusPlus2a) {
|
|
|
|
++NumLangDialectMismatch;
|
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::lang_dialect_mismatch);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
if (const T *ResultDecl = findDefInDeclContext<T>(TU, LookupName))
|
2019-07-18 23:23:10 +08:00
|
|
|
return importDefinition(ResultDecl, Unit);
|
2017-09-22 19:11:01 +08:00
|
|
|
return llvm::make_error<IndexError>(index_error_code::failed_import);
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
llvm::Expected<const FunctionDecl *>
|
|
|
|
CrossTranslationUnitContext::getCrossTUDefinition(const FunctionDecl *FD,
|
|
|
|
StringRef CrossTUDir,
|
|
|
|
StringRef IndexName,
|
|
|
|
bool DisplayCTUProgress) {
|
|
|
|
return getCrossTUDefinitionImpl(FD, CrossTUDir, IndexName,
|
|
|
|
DisplayCTUProgress);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<const VarDecl *>
|
|
|
|
CrossTranslationUnitContext::getCrossTUDefinition(const VarDecl *VD,
|
|
|
|
StringRef CrossTUDir,
|
|
|
|
StringRef IndexName,
|
|
|
|
bool DisplayCTUProgress) {
|
|
|
|
return getCrossTUDefinitionImpl(VD, CrossTUDir, IndexName,
|
|
|
|
DisplayCTUProgress);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) {
|
|
|
|
switch (IE.getCode()) {
|
|
|
|
case index_error_code::missing_index_file:
|
2018-12-14 11:35:10 +08:00
|
|
|
Context.getDiagnostics().Report(diag::err_ctu_error_opening)
|
|
|
|
<< IE.getFileName();
|
2017-09-22 19:11:01 +08:00
|
|
|
break;
|
|
|
|
case index_error_code::invalid_index_format:
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
|
2017-09-22 19:11:01 +08:00
|
|
|
<< IE.getFileName() << IE.getLineNum();
|
2017-09-24 23:17:46 +08:00
|
|
|
break;
|
2017-09-22 19:11:01 +08:00
|
|
|
case index_error_code::multiple_definitions:
|
|
|
|
Context.getDiagnostics().Report(diag::err_multiple_def_index)
|
|
|
|
<< IE.getLineNum();
|
|
|
|
break;
|
2018-12-08 00:32:43 +08:00
|
|
|
case index_error_code::triple_mismatch:
|
|
|
|
Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
|
|
|
|
<< IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
|
|
|
|
break;
|
2017-09-22 19:11:01 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
|
[CTU] Add DisplayCTUProgress analyzer switch
Summary:
With a new switch we may be able to print to stderr if a new TU is being loaded
during CTU. This is very important for higher level scripts (like CodeChecker)
to be able to parse this output so they can create e.g. a zip file in case of
a Clang crash which contains all the related TU files.
Reviewers: xazax.hun, Szelethus, a_sidorin, george.karpenkov
Subscribers: whisperity, baloghadamsoftware, szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp,
Differential Revision: https://reviews.llvm.org/D55135
llvm-svn: 348594
2018-12-07 22:56:02 +08:00
|
|
|
StringRef LookupName, StringRef CrossTUDir, StringRef IndexName,
|
|
|
|
bool DisplayCTUProgress) {
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
// FIXME: The current implementation only supports loading decls with
|
2017-09-22 19:11:01 +08:00
|
|
|
// a lookup name from a single translation unit. If multiple
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
// translation units contains decls with the same lookup name an
|
2017-09-22 19:11:01 +08:00
|
|
|
// error will be returned.
|
2019-07-08 20:37:10 +08:00
|
|
|
|
|
|
|
if (NumASTLoaded >= CTULoadThreshold) {
|
|
|
|
++NumASTLoadThresholdReached;
|
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::load_threshold_reached);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
ASTUnit *Unit = nullptr;
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
auto NameUnitCacheEntry = NameASTUnitMap.find(LookupName);
|
|
|
|
if (NameUnitCacheEntry == NameASTUnitMap.end()) {
|
|
|
|
if (NameFileMap.empty()) {
|
2017-09-22 19:11:01 +08:00
|
|
|
SmallString<256> IndexFile = CrossTUDir;
|
|
|
|
if (llvm::sys::path::is_absolute(IndexName))
|
|
|
|
IndexFile = IndexName;
|
|
|
|
else
|
|
|
|
llvm::sys::path::append(IndexFile, IndexName);
|
|
|
|
llvm::Expected<llvm::StringMap<std::string>> IndexOrErr =
|
|
|
|
parseCrossTUIndex(IndexFile, CrossTUDir);
|
|
|
|
if (IndexOrErr)
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
NameFileMap = *IndexOrErr;
|
2017-09-22 19:11:01 +08:00
|
|
|
else
|
|
|
|
return IndexOrErr.takeError();
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
auto It = NameFileMap.find(LookupName);
|
|
|
|
if (It == NameFileMap.end()) {
|
2018-12-07 19:55:22 +08:00
|
|
|
++NumNotInOtherTU;
|
2017-09-22 19:11:01 +08:00
|
|
|
return llvm::make_error<IndexError>(index_error_code::missing_definition);
|
2018-12-07 19:55:22 +08:00
|
|
|
}
|
2017-09-22 19:11:01 +08:00
|
|
|
StringRef ASTFileName = It->second;
|
|
|
|
auto ASTCacheEntry = FileASTUnitMap.find(ASTFileName);
|
|
|
|
if (ASTCacheEntry == FileASTUnitMap.end()) {
|
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
TextDiagnosticPrinter *DiagClient =
|
|
|
|
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
|
|
|
|
new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
|
|
|
|
|
|
|
|
std::unique_ptr<ASTUnit> LoadedUnit(ASTUnit::LoadFromASTFile(
|
|
|
|
ASTFileName, CI.getPCHContainerOperations()->getRawReader(),
|
|
|
|
ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts()));
|
|
|
|
Unit = LoadedUnit.get();
|
|
|
|
FileASTUnitMap[ASTFileName] = std::move(LoadedUnit);
|
2019-07-08 20:37:10 +08:00
|
|
|
++NumASTLoaded;
|
[CTU] Add DisplayCTUProgress analyzer switch
Summary:
With a new switch we may be able to print to stderr if a new TU is being loaded
during CTU. This is very important for higher level scripts (like CodeChecker)
to be able to parse this output so they can create e.g. a zip file in case of
a Clang crash which contains all the related TU files.
Reviewers: xazax.hun, Szelethus, a_sidorin, george.karpenkov
Subscribers: whisperity, baloghadamsoftware, szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp,
Differential Revision: https://reviews.llvm.org/D55135
llvm-svn: 348594
2018-12-07 22:56:02 +08:00
|
|
|
if (DisplayCTUProgress) {
|
|
|
|
llvm::errs() << "CTU loaded AST file: "
|
|
|
|
<< ASTFileName << "\n";
|
|
|
|
}
|
2017-09-22 19:11:01 +08:00
|
|
|
} else {
|
|
|
|
Unit = ASTCacheEntry->second.get();
|
|
|
|
}
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
NameASTUnitMap[LookupName] = Unit;
|
2017-09-22 19:11:01 +08:00
|
|
|
} else {
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
Unit = NameUnitCacheEntry->second;
|
2017-09-22 19:11:01 +08:00
|
|
|
}
|
2019-01-07 22:05:19 +08:00
|
|
|
if (!Unit)
|
|
|
|
return llvm::make_error<IndexError>(
|
|
|
|
index_error_code::failed_to_get_external_ast);
|
2017-09-22 19:11:01 +08:00
|
|
|
return Unit;
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
template <typename T>
|
|
|
|
llvm::Expected<const T *>
|
2019-07-18 23:23:10 +08:00
|
|
|
CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
|
2018-12-07 20:21:43 +08:00
|
|
|
|
2019-07-18 23:23:10 +08:00
|
|
|
assert(&D->getASTContext() == &Unit->getASTContext() &&
|
|
|
|
"ASTContext of Decl and the unit should match.");
|
|
|
|
ASTImporter &Importer = getOrCreateASTImporter(Unit);
|
|
|
|
|
[ASTImporter] Use llvm::Expected and Error in the importer API
Summary:
This is the final phase of the refactoring towards using llvm::Expected
and llvm::Error in the ASTImporter API.
This involves the following:
- remove old Import functions which returned with a pointer,
- use the Import_New functions (which return with Err or Expected) everywhere
and handle their return value
- rename Import_New functions to Import
This affects both Clang and LLDB.
Reviewers: shafik, teemperor, aprantl, a_sidorin, balazske, a.sidorin
Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits
Tags: #clang, #lldb
Differential Revision: https://reviews.llvm.org/D61438
llvm-svn: 360760
2019-05-15 18:29:48 +08:00
|
|
|
auto ToDeclOrError = Importer.Import(D);
|
2019-04-08 21:59:15 +08:00
|
|
|
if (!ToDeclOrError) {
|
|
|
|
handleAllErrors(ToDeclOrError.takeError(),
|
|
|
|
[&](const ImportError &IE) {
|
|
|
|
switch (IE.Error) {
|
|
|
|
case ImportError::NameConflict:
|
2019-06-28 16:08:51 +08:00
|
|
|
++NumNameConflicts;
|
2019-04-08 21:59:15 +08:00
|
|
|
break;
|
|
|
|
case ImportError::UnsupportedConstruct:
|
2019-06-28 16:08:51 +08:00
|
|
|
++NumUnsupportedNodeFound;
|
2019-04-08 21:59:15 +08:00
|
|
|
break;
|
|
|
|
case ImportError::Unknown:
|
|
|
|
llvm_unreachable("Unknown import error happened.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-12-08 00:05:58 +08:00
|
|
|
return llvm::make_error<IndexError>(index_error_code::failed_import);
|
2019-04-08 21:59:15 +08:00
|
|
|
}
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
auto *ToDecl = cast<T>(*ToDeclOrError);
|
|
|
|
assert(hasBodyOrInit(ToDecl) && "Imported Decl should have body or init.");
|
2018-12-07 19:55:22 +08:00
|
|
|
++NumGetCTUSuccess;
|
2019-04-08 21:59:15 +08:00
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
return ToDecl;
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
llvm::Expected<const FunctionDecl *>
|
2019-07-18 23:23:10 +08:00
|
|
|
CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD,
|
|
|
|
ASTUnit *Unit) {
|
|
|
|
return importDefinitionImpl(FD, Unit);
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<const VarDecl *>
|
2019-07-18 23:23:10 +08:00
|
|
|
CrossTranslationUnitContext::importDefinition(const VarDecl *VD,
|
|
|
|
ASTUnit *Unit) {
|
|
|
|
return importDefinitionImpl(VD, Unit);
|
[analyzer][CrossTU] Extend CTU to VarDecls with initializer
Summary:
The existing CTU mechanism imports `FunctionDecl`s where the definition is available in another TU. This patch extends that to VarDecls, to bind more constants.
- Add VarDecl importing functionality to CrossTranslationUnitContext
- Import Decls while traversing them in AnalysisConsumer
- Add VarDecls to CTU external mappings generator
- Name changes from "external function map" to "external definition map"
Reviewers: NoQ, dcoughlin, xazax.hun, george.karpenkov, martong
Reviewed By: xazax.hun
Subscribers: Charusso, baloghadamsoftware, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, george.karpenkov, mgorny, whisperity, szepet, rnkovacs, a.sidorin, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D46421
llvm-svn: 358968
2019-04-23 19:04:41 +08:00
|
|
|
}
|
|
|
|
|
2019-07-01 23:37:07 +08:00
|
|
|
void CrossTranslationUnitContext::lazyInitImporterSharedSt(
|
2018-12-17 21:53:12 +08:00
|
|
|
TranslationUnitDecl *ToTU) {
|
2019-07-01 23:37:07 +08:00
|
|
|
if (!ImporterSharedSt)
|
|
|
|
ImporterSharedSt = std::make_shared<ASTImporterSharedState>(*ToTU);
|
2018-12-17 21:53:12 +08:00
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
ASTImporter &
|
2019-07-18 23:23:10 +08:00
|
|
|
CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) {
|
|
|
|
ASTContext &From = Unit->getASTContext();
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
|
|
|
|
if (I != ASTUnitImporterMap.end())
|
|
|
|
return *I->second;
|
2019-07-01 23:37:07 +08:00
|
|
|
lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
|
2019-07-18 23:23:10 +08:00
|
|
|
ASTImporter *NewImporter =
|
|
|
|
new ASTImporter(Context, Context.getSourceManager().getFileManager(),
|
|
|
|
*Unit, false, ImporterSharedSt);
|
2017-09-22 19:11:01 +08:00
|
|
|
ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
|
|
|
|
return *NewImporter;
|
|
|
|
}
|
|
|
|
|
2019-07-18 23:23:10 +08:00
|
|
|
llvm::Optional<std::pair<SourceLocation, ASTUnit *>>
|
|
|
|
CrossTranslationUnitContext::getImportedFromSourceLocation(
|
|
|
|
const clang::SourceLocation &ToLoc) const {
|
|
|
|
if (!ImporterSharedSt)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const SourceManager &SM = Context.getSourceManager();
|
|
|
|
auto DecToLoc = SM.getDecomposedLoc(ToLoc);
|
|
|
|
|
|
|
|
auto I = ImporterSharedSt->getImportedFileIDs().find(DecToLoc.first);
|
|
|
|
if (I == ImporterSharedSt->getImportedFileIDs().end())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
FileID FromID = I->second.first;
|
|
|
|
clang::ASTUnit *Unit = I->second.second;
|
|
|
|
SourceLocation FromLoc =
|
|
|
|
Unit->getSourceManager().getComposedLoc(FromID, DecToLoc.second);
|
|
|
|
|
|
|
|
return std::make_pair(FromLoc, Unit);
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:11:01 +08:00
|
|
|
} // namespace cross_tu
|
|
|
|
} // namespace clang
|