2017-04-12 03:33:35 +08:00
|
|
|
//===- ExternalASTMerger.cpp - Merging External AST Interface ---*- 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-04-12 03:33:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the ExternalASTMerger, which vends a combination of
|
|
|
|
// ASTs from several different ASTContext/FileManager pairs
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2017-09-28 03:57:58 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2017-04-12 03:33:35 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2018-01-26 19:36:54 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2017-04-12 03:33:35 +08:00
|
|
|
#include "clang/AST/ExternalASTMerger.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename T> struct Source {
|
|
|
|
T t;
|
2017-04-12 04:51:21 +08:00
|
|
|
Source(T t) : t(t) {}
|
2017-04-12 03:33:35 +08:00
|
|
|
operator T() { return t; }
|
|
|
|
template <typename U = T> U &get() { return t; }
|
|
|
|
template <typename U = T> const U &get() const { return t; }
|
|
|
|
template <typename U> operator Source<U>() { return Source<U>(t); }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::pair<Source<NamedDecl *>, ASTImporter *> Candidate;
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
/// For the given DC, return the DC that is safe to perform lookups on. This is
|
|
|
|
/// the DC we actually want to work with most of the time.
|
|
|
|
const DeclContext *CanonicalizeDC(const DeclContext *DC) {
|
|
|
|
if (isa<LinkageSpecDecl>(DC))
|
|
|
|
return DC->getRedeclContext();
|
|
|
|
return DC;
|
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
|
|
|
|
Source<const DeclContext *>
|
|
|
|
LookupSameContext(Source<TranslationUnitDecl *> SourceTU, const DeclContext *DC,
|
|
|
|
ASTImporter &ReverseImporter) {
|
2017-09-28 03:57:58 +08:00
|
|
|
DC = CanonicalizeDC(DC);
|
2017-04-12 03:33:35 +08:00
|
|
|
if (DC->isTranslationUnit()) {
|
|
|
|
return SourceTU;
|
|
|
|
}
|
|
|
|
Source<const DeclContext *> SourceParentDC =
|
|
|
|
LookupSameContext(SourceTU, DC->getParent(), ReverseImporter);
|
|
|
|
if (!SourceParentDC) {
|
|
|
|
// If we couldn't find the parent DC in this TranslationUnit, give up.
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
auto *ND = cast<NamedDecl>(DC);
|
2017-04-12 03:33:35 +08:00
|
|
|
DeclarationName Name = ND->getDeclName();
|
[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 SourceNameOrErr = ReverseImporter.Import(Name);
|
2019-04-08 21:59:15 +08:00
|
|
|
if (!SourceNameOrErr) {
|
|
|
|
llvm::consumeError(SourceNameOrErr.takeError());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Source<DeclarationName> SourceName = *SourceNameOrErr;
|
2017-04-12 03:33:35 +08:00
|
|
|
DeclContext::lookup_result SearchResult =
|
|
|
|
SourceParentDC.get()->lookup(SourceName.get());
|
|
|
|
size_t SearchResultSize = SearchResult.size();
|
2017-09-28 03:57:58 +08:00
|
|
|
if (SearchResultSize == 0 || SearchResultSize > 1) {
|
|
|
|
// There are two cases here. First, we might not find the name.
|
|
|
|
// We might also find multiple copies, in which case we have no
|
|
|
|
// guarantee that the one we wanted is the one we pick. (E.g.,
|
|
|
|
// if we have two specializations of the same template it is
|
|
|
|
// very hard to determine which is the one you want.)
|
|
|
|
//
|
|
|
|
// The Origins map fixes this problem by allowing the origin to be
|
|
|
|
// explicitly recorded, so we trigger that recording by returning
|
|
|
|
// nothing (rather than a possibly-inaccurate guess) here.
|
2017-04-12 03:33:35 +08:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
NamedDecl *SearchResultDecl = SearchResult[0];
|
2017-09-28 03:57:58 +08:00
|
|
|
if (isa<DeclContext>(SearchResultDecl) &&
|
|
|
|
SearchResultDecl->getKind() == DC->getDeclKind())
|
|
|
|
return cast<DeclContext>(SearchResultDecl)->getPrimaryContext();
|
|
|
|
return nullptr; // This type of lookup is unsupported
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
/// A custom implementation of ASTImporter, for ExternalASTMerger's purposes.
|
|
|
|
///
|
|
|
|
/// There are several modifications:
|
|
|
|
///
|
|
|
|
/// - It enables lazy lookup (via the HasExternalLexicalStorage flag and a few
|
|
|
|
/// others), which instructs Clang to refer to ExternalASTMerger. Also, it
|
|
|
|
/// forces MinimalImport to true, which is necessary to make this work.
|
|
|
|
/// - It maintains a reverse importer for use with names. This allows lookup of
|
|
|
|
/// arbitrary names in the source context.
|
|
|
|
/// - It updates the ExternalASTMerger's origin map as needed whenever a
|
|
|
|
/// it sees a DeclContext.
|
|
|
|
class LazyASTImporter : public ASTImporter {
|
|
|
|
private:
|
|
|
|
ExternalASTMerger &Parent;
|
|
|
|
ASTImporter Reverse;
|
|
|
|
const ExternalASTMerger::OriginMap &FromOrigins;
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
/// @see ExternalASTMerger::ImporterSource::Temporary
|
|
|
|
bool TemporarySource;
|
|
|
|
/// Map of imported declarations back to the declarations they originated
|
|
|
|
/// from.
|
|
|
|
llvm::DenseMap<Decl *, Decl *> ToOrigin;
|
|
|
|
/// @see ExternalASTMerger::ImporterSource::Merger
|
|
|
|
ExternalASTMerger *SourceMerger;
|
2017-09-28 03:57:58 +08:00
|
|
|
llvm::raw_ostream &logs() { return Parent.logs(); }
|
|
|
|
public:
|
|
|
|
LazyASTImporter(ExternalASTMerger &_Parent, ASTContext &ToContext,
|
2019-10-01 17:02:05 +08:00
|
|
|
FileManager &ToFileManager,
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
const ExternalASTMerger::ImporterSource &S,
|
2019-09-30 16:52:16 +08:00
|
|
|
std::shared_ptr<ASTImporterSharedState> SharedState)
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
: ASTImporter(ToContext, ToFileManager, S.getASTContext(),
|
|
|
|
S.getFileManager(),
|
2019-09-30 16:52:16 +08:00
|
|
|
/*MinimalImport=*/true, SharedState),
|
2019-10-01 17:02:05 +08:00
|
|
|
Parent(_Parent),
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
Reverse(S.getASTContext(), S.getFileManager(), ToContext, ToFileManager,
|
|
|
|
/*MinimalImport=*/true),
|
|
|
|
FromOrigins(S.getOriginMap()), TemporarySource(S.isTemporary()),
|
|
|
|
SourceMerger(S.getMerger()) {}
|
|
|
|
|
|
|
|
llvm::Expected<Decl *> ImportImpl(Decl *FromD) override {
|
|
|
|
if (!TemporarySource || !SourceMerger)
|
|
|
|
return ASTImporter::ImportImpl(FromD);
|
|
|
|
|
|
|
|
// If we get here, then this source is importing from a temporary ASTContext
|
|
|
|
// that also has another ExternalASTMerger attached. It could be
|
|
|
|
// possible that the current ExternalASTMerger and the temporary ASTContext
|
|
|
|
// share a common ImporterSource, which means that the temporary
|
|
|
|
// AST could contain declarations that were imported from a source
|
|
|
|
// that this ExternalASTMerger can access directly. Instead of importing
|
|
|
|
// such declarations from the temporary ASTContext, they should instead
|
|
|
|
// be directly imported by this ExternalASTMerger from the original
|
|
|
|
// source. This way the ExternalASTMerger can safely do a minimal import
|
|
|
|
// without creating incomplete declarations originated from a temporary
|
|
|
|
// ASTContext. If we would try to complete such declarations later on, we
|
|
|
|
// would fail to do so as their temporary AST could be deleted (which means
|
|
|
|
// that the missing parts of the minimally imported declaration in that
|
|
|
|
// ASTContext were also deleted).
|
|
|
|
//
|
|
|
|
// The following code tracks back any declaration that needs to be
|
|
|
|
// imported from the temporary ASTContext to a persistent ASTContext.
|
|
|
|
// Then the ExternalASTMerger tries to import from the persistent
|
|
|
|
// ASTContext directly by using the associated ASTImporter. If that
|
|
|
|
// succeeds, this ASTImporter just maps the declarations imported by
|
|
|
|
// the other (persistent) ASTImporter to this (temporary) ASTImporter.
|
|
|
|
// The steps can be visualized like this:
|
|
|
|
//
|
|
|
|
// Target AST <--- 3. Indirect import --- Persistent AST
|
|
|
|
// ^ of persistent decl ^
|
|
|
|
// | |
|
|
|
|
// 1. Current import 2. Tracking back to persistent decl
|
|
|
|
// 4. Map persistent decl |
|
|
|
|
// & pretend we imported. |
|
|
|
|
// | |
|
|
|
|
// Temporary AST -------------------------------'
|
|
|
|
|
|
|
|
// First, ask the ExternalASTMerger of the source where the temporary
|
|
|
|
// declaration originated from.
|
|
|
|
Decl *Persistent = SourceMerger->FindOriginalDecl(FromD);
|
|
|
|
// FromD isn't from a persistent AST, so just do a normal import.
|
|
|
|
if (!Persistent)
|
|
|
|
return ASTImporter::ImportImpl(FromD);
|
|
|
|
// Now ask the current ExternalASTMerger to try import the persistent
|
|
|
|
// declaration into the target.
|
|
|
|
ASTContext &PersistentCtx = Persistent->getASTContext();
|
|
|
|
ASTImporter &OtherImporter = Parent.ImporterForOrigin(PersistentCtx);
|
|
|
|
// Check that we never end up in the current Importer again.
|
|
|
|
assert((&PersistentCtx != &getFromContext()) && (&OtherImporter != this) &&
|
|
|
|
"Delegated to same Importer?");
|
|
|
|
auto DeclOrErr = OtherImporter.Import(Persistent);
|
|
|
|
// Errors when importing the persistent decl are treated as if we
|
|
|
|
// had errors with importing the temporary decl.
|
|
|
|
if (!DeclOrErr)
|
|
|
|
return DeclOrErr.takeError();
|
|
|
|
Decl *D = *DeclOrErr;
|
|
|
|
// Tell the current ASTImporter that this has already been imported
|
|
|
|
// to prevent any further queries for the temporary decl.
|
|
|
|
MapImported(FromD, D);
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implements the ASTImporter interface for tracking back a declaration
|
|
|
|
/// to its original declaration it came from.
|
|
|
|
Decl *GetOriginalDecl(Decl *To) override {
|
|
|
|
auto It = ToOrigin.find(To);
|
|
|
|
if (It != ToOrigin.end())
|
|
|
|
return It->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
|
|
|
|
/// Whenever a DeclContext is imported, ensure that ExternalASTSource's origin
|
|
|
|
/// map is kept up to date. Also set the appropriate flags.
|
2019-03-21 03:00:25 +08:00
|
|
|
void Imported(Decl *From, Decl *To) override {
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
ToOrigin[To] = From;
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
if (auto *ToDC = dyn_cast<DeclContext>(To)) {
|
|
|
|
const bool LoggingEnabled = Parent.LoggingEnabled();
|
|
|
|
if (LoggingEnabled)
|
|
|
|
logs() << "(ExternalASTMerger*)" << (void*)&Parent
|
|
|
|
<< " imported (DeclContext*)" << (void*)ToDC
|
|
|
|
<< ", (ASTContext*)" << (void*)&getToContext()
|
|
|
|
<< " from (DeclContext*)" << (void*)llvm::cast<DeclContext>(From)
|
|
|
|
<< ", (ASTContext*)" << (void*)&getFromContext()
|
|
|
|
<< "\n";
|
|
|
|
Source<DeclContext *> FromDC(
|
|
|
|
cast<DeclContext>(From)->getPrimaryContext());
|
|
|
|
if (FromOrigins.count(FromDC) &&
|
|
|
|
Parent.HasImporterForOrigin(*FromOrigins.at(FromDC).AST)) {
|
|
|
|
if (LoggingEnabled)
|
|
|
|
logs() << "(ExternalASTMerger*)" << (void*)&Parent
|
|
|
|
<< " forced origin (DeclContext*)"
|
|
|
|
<< (void*)FromOrigins.at(FromDC).DC
|
|
|
|
<< ", (ASTContext*)"
|
|
|
|
<< (void*)FromOrigins.at(FromDC).AST
|
|
|
|
<< "\n";
|
|
|
|
Parent.ForceRecordOrigin(ToDC, FromOrigins.at(FromDC));
|
|
|
|
} else {
|
|
|
|
if (LoggingEnabled)
|
|
|
|
logs() << "(ExternalASTMerger*)" << (void*)&Parent
|
|
|
|
<< " maybe recording origin (DeclContext*)" << (void*)FromDC
|
|
|
|
<< ", (ASTContext*)" << (void*)&getFromContext()
|
|
|
|
<< "\n";
|
|
|
|
Parent.MaybeRecordOrigin(ToDC, {FromDC, &getFromContext()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto *ToTag = dyn_cast<TagDecl>(To)) {
|
|
|
|
ToTag->setHasExternalLexicalStorage();
|
2018-11-29 21:50:30 +08:00
|
|
|
ToTag->getPrimaryContext()->setMustBuildLookupTable();
|
2017-09-28 03:57:58 +08:00
|
|
|
assert(Parent.CanComplete(ToTag));
|
|
|
|
} else if (auto *ToNamespace = dyn_cast<NamespaceDecl>(To)) {
|
|
|
|
ToNamespace->setHasExternalVisibleStorage();
|
|
|
|
assert(Parent.CanComplete(ToNamespace));
|
|
|
|
} else if (auto *ToContainer = dyn_cast<ObjCContainerDecl>(To)) {
|
|
|
|
ToContainer->setHasExternalLexicalStorage();
|
2018-11-29 21:50:30 +08:00
|
|
|
ToContainer->getPrimaryContext()->setMustBuildLookupTable();
|
2017-09-28 03:57:58 +08:00
|
|
|
assert(Parent.CanComplete(ToContainer));
|
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
ASTImporter &GetReverse() { return Reverse; }
|
|
|
|
};
|
2017-04-12 03:33:35 +08:00
|
|
|
|
|
|
|
bool HasDeclOfSameType(llvm::ArrayRef<Candidate> Decls, const Candidate &C) {
|
2017-09-28 03:57:58 +08:00
|
|
|
if (isa<FunctionDecl>(C.first.get()))
|
|
|
|
return false;
|
2017-04-18 01:16:19 +08:00
|
|
|
return llvm::any_of(Decls, [&](const Candidate &D) {
|
2017-04-12 03:33:35 +08:00
|
|
|
return C.first.get()->getKind() == D.first.get()->getKind();
|
|
|
|
});
|
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
|
2017-04-12 03:33:35 +08:00
|
|
|
} // end namespace
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
ASTImporter &ExternalASTMerger::ImporterForOrigin(ASTContext &OriginContext) {
|
|
|
|
for (const std::unique_ptr<ASTImporter> &I : Importers)
|
|
|
|
if (&I->getFromContext() == &OriginContext)
|
|
|
|
return *I;
|
|
|
|
llvm_unreachable("We should have an importer for this origin!");
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
namespace {
|
|
|
|
LazyASTImporter &LazyImporterForOrigin(ExternalASTMerger &Merger,
|
|
|
|
ASTContext &OriginContext) {
|
|
|
|
return static_cast<LazyASTImporter &>(
|
|
|
|
Merger.ImporterForOrigin(OriginContext));
|
|
|
|
}
|
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
bool ExternalASTMerger::HasImporterForOrigin(ASTContext &OriginContext) {
|
|
|
|
for (const std::unique_ptr<ASTImporter> &I : Importers)
|
|
|
|
if (&I->getFromContext() == &OriginContext)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename CallbackType>
|
|
|
|
void ExternalASTMerger::ForEachMatchingDC(const DeclContext *DC,
|
|
|
|
CallbackType Callback) {
|
|
|
|
if (Origins.count(DC)) {
|
|
|
|
ExternalASTMerger::DCOrigin Origin = Origins[DC];
|
|
|
|
LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
|
|
|
|
Callback(Importer, Importer.GetReverse(), Origin.DC);
|
|
|
|
} else {
|
|
|
|
bool DidCallback = false;
|
|
|
|
for (const std::unique_ptr<ASTImporter> &Importer : Importers) {
|
|
|
|
Source<TranslationUnitDecl *> SourceTU =
|
|
|
|
Importer->getFromContext().getTranslationUnitDecl();
|
|
|
|
ASTImporter &Reverse =
|
|
|
|
static_cast<LazyASTImporter *>(Importer.get())->GetReverse();
|
|
|
|
if (auto SourceDC = LookupSameContext(SourceTU, DC, Reverse)) {
|
|
|
|
DidCallback = true;
|
|
|
|
if (Callback(*Importer, Reverse, SourceDC))
|
|
|
|
break;
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
if (!DidCallback && LoggingEnabled())
|
|
|
|
logs() << "(ExternalASTMerger*)" << (void*)this
|
2017-09-28 23:44:46 +08:00
|
|
|
<< " asserting for (DeclContext*)" << (const void*)DC
|
2017-09-28 03:57:58 +08:00
|
|
|
<< ", (ASTContext*)" << (void*)&Target.AST
|
|
|
|
<< "\n";
|
|
|
|
assert(DidCallback && "Couldn't find a source context matching our DC");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::CompleteType(TagDecl *Tag) {
|
|
|
|
assert(Tag->hasExternalLexicalStorage());
|
|
|
|
ForEachMatchingDC(Tag, [&](ASTImporter &Forward, ASTImporter &Reverse,
|
|
|
|
Source<const DeclContext *> SourceDC) -> bool {
|
|
|
|
auto *SourceTag = const_cast<TagDecl *>(cast<TagDecl>(SourceDC.get()));
|
|
|
|
if (SourceTag->hasExternalLexicalStorage())
|
|
|
|
SourceTag->getASTContext().getExternalSource()->CompleteType(SourceTag);
|
|
|
|
if (!SourceTag->getDefinition())
|
|
|
|
return false;
|
[ASTImporter] Refactor Decl creation
Summary:
Generalize the creation of Decl nodes during Import. With this patch we do the
same things after and before a new AST node is created (::Create) The import
logic should be really simple, we create the node, then we mark that as
imported, then we recursively import the parts for that node and then set them
on that node. However, the AST is actually a graph, so we have to handle
circles. If we mark something as imported (`MapImported()`) then we return with
the corresponding `To` decl whenever we want to import that node again, this way
circles are handled. In order to make this algorithm work we must ensure
things, which are handled in the generic CreateDecl<> template:
* There are no `Import()` calls in between any node creation (::Create)
and the `MapImported()` call.
* Before actually creating an AST node (::Create), we must check if
the Node had been imported already, if yes then return with that one.
One very important case for this is connected to templates: we may
start an import both from the templated decl of a template and from
the template itself.
Now, the virtual `Imported` function is called in `ASTImporter::Impor(Decl *)`,
but only once, when the `Decl` is imported. One point of this refactor is to
separate responsibilities. The original `Imported()` had 3 responsibilities:
- notify subclasses when an import happened
- register the decl into `ImportedDecls`
- initialise the Decl (set attributes, etc)
Now all of these are in separate functions:
- `Imported`
- `MapImported`
- `InitializeImportedDecl`
I tried to check all the clients, I executed tests for `ExternalASTMerger.cpp`
and some unittests for lldb.
Reviewers: a.sidorin, balazske, xazax.hun, r.stahl
Subscribers: rnkovacs, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D47632
llvm-svn: 336896
2018-07-12 17:42:05 +08:00
|
|
|
Forward.MapImported(SourceTag, Tag);
|
[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
|
|
|
if (llvm::Error Err = Forward.ImportDefinition(SourceTag))
|
2018-10-19 21:32:20 +08:00
|
|
|
llvm::consumeError(std::move(Err));
|
2017-09-28 03:57:58 +08:00
|
|
|
Tag->setCompleteDefinition(SourceTag->isCompleteDefinition());
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
void ExternalASTMerger::CompleteType(ObjCInterfaceDecl *Interface) {
|
|
|
|
assert(Interface->hasExternalLexicalStorage());
|
2017-04-18 01:16:19 +08:00
|
|
|
ForEachMatchingDC(
|
2017-09-28 03:57:58 +08:00
|
|
|
Interface, [&](ASTImporter &Forward, ASTImporter &Reverse,
|
|
|
|
Source<const DeclContext *> SourceDC) -> bool {
|
|
|
|
auto *SourceInterface = const_cast<ObjCInterfaceDecl *>(
|
|
|
|
cast<ObjCInterfaceDecl>(SourceDC.get()));
|
|
|
|
if (SourceInterface->hasExternalLexicalStorage())
|
|
|
|
SourceInterface->getASTContext().getExternalSource()->CompleteType(
|
|
|
|
SourceInterface);
|
|
|
|
if (!SourceInterface->getDefinition())
|
|
|
|
return false;
|
[ASTImporter] Refactor Decl creation
Summary:
Generalize the creation of Decl nodes during Import. With this patch we do the
same things after and before a new AST node is created (::Create) The import
logic should be really simple, we create the node, then we mark that as
imported, then we recursively import the parts for that node and then set them
on that node. However, the AST is actually a graph, so we have to handle
circles. If we mark something as imported (`MapImported()`) then we return with
the corresponding `To` decl whenever we want to import that node again, this way
circles are handled. In order to make this algorithm work we must ensure
things, which are handled in the generic CreateDecl<> template:
* There are no `Import()` calls in between any node creation (::Create)
and the `MapImported()` call.
* Before actually creating an AST node (::Create), we must check if
the Node had been imported already, if yes then return with that one.
One very important case for this is connected to templates: we may
start an import both from the templated decl of a template and from
the template itself.
Now, the virtual `Imported` function is called in `ASTImporter::Impor(Decl *)`,
but only once, when the `Decl` is imported. One point of this refactor is to
separate responsibilities. The original `Imported()` had 3 responsibilities:
- notify subclasses when an import happened
- register the decl into `ImportedDecls`
- initialise the Decl (set attributes, etc)
Now all of these are in separate functions:
- `Imported`
- `MapImported`
- `InitializeImportedDecl`
I tried to check all the clients, I executed tests for `ExternalASTMerger.cpp`
and some unittests for lldb.
Reviewers: a.sidorin, balazske, xazax.hun, r.stahl
Subscribers: rnkovacs, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D47632
llvm-svn: 336896
2018-07-12 17:42:05 +08:00
|
|
|
Forward.MapImported(SourceInterface, Interface);
|
[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
|
|
|
if (llvm::Error Err = Forward.ImportDefinition(SourceInterface))
|
2018-10-19 21:32:20 +08:00
|
|
|
llvm::consumeError(std::move(Err));
|
2017-09-28 03:57:58 +08:00
|
|
|
return true;
|
2017-04-18 01:16:19 +08:00
|
|
|
});
|
2017-09-28 03:57:58 +08:00
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
bool ExternalASTMerger::CanComplete(DeclContext *Interface) {
|
|
|
|
assert(Interface->hasExternalLexicalStorage() ||
|
|
|
|
Interface->hasExternalVisibleStorage());
|
|
|
|
bool FoundMatchingDC = false;
|
|
|
|
ForEachMatchingDC(Interface,
|
|
|
|
[&](ASTImporter &Forward, ASTImporter &Reverse,
|
|
|
|
Source<const DeclContext *> SourceDC) -> bool {
|
|
|
|
FoundMatchingDC = true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return FoundMatchingDC;
|
|
|
|
}
|
2017-04-12 03:33:35 +08:00
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
namespace {
|
|
|
|
bool IsSameDC(const DeclContext *D1, const DeclContext *D2) {
|
|
|
|
if (isa<ObjCContainerDecl>(D1) && isa<ObjCContainerDecl>(D2))
|
|
|
|
return true; // There are many cases where Objective-C is ambiguous.
|
|
|
|
if (auto *T1 = dyn_cast<TagDecl>(D1))
|
|
|
|
if (auto *T2 = dyn_cast<TagDecl>(D2))
|
|
|
|
if (T1->getFirstDecl() == T2->getFirstDecl())
|
|
|
|
return true;
|
|
|
|
return D1 == D2 || D1 == CanonicalizeDC(D2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::MaybeRecordOrigin(const DeclContext *ToDC,
|
|
|
|
DCOrigin Origin) {
|
|
|
|
LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
|
|
|
|
ASTImporter &Reverse = Importer.GetReverse();
|
|
|
|
Source<const DeclContext *> FoundFromDC =
|
|
|
|
LookupSameContext(Origin.AST->getTranslationUnitDecl(), ToDC, Reverse);
|
|
|
|
const bool DoRecord = !FoundFromDC || !IsSameDC(FoundFromDC.get(), Origin.DC);
|
|
|
|
if (DoRecord)
|
|
|
|
RecordOriginImpl(ToDC, Origin, Importer);
|
|
|
|
if (LoggingEnabled())
|
|
|
|
logs() << "(ExternalASTMerger*)" << (void*)this
|
|
|
|
<< (DoRecord ? " decided " : " decided NOT")
|
|
|
|
<< " to record origin (DeclContext*)" << (void*)Origin.DC
|
|
|
|
<< ", (ASTContext*)" << (void*)&Origin.AST
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::ForceRecordOrigin(const DeclContext *ToDC,
|
|
|
|
DCOrigin Origin) {
|
|
|
|
RecordOriginImpl(ToDC, Origin, ImporterForOrigin(*Origin.AST));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::RecordOriginImpl(const DeclContext *ToDC, DCOrigin Origin,
|
|
|
|
ASTImporter &Importer) {
|
|
|
|
Origins[ToDC] = Origin;
|
[ASTImporter] Refactor Decl creation
Summary:
Generalize the creation of Decl nodes during Import. With this patch we do the
same things after and before a new AST node is created (::Create) The import
logic should be really simple, we create the node, then we mark that as
imported, then we recursively import the parts for that node and then set them
on that node. However, the AST is actually a graph, so we have to handle
circles. If we mark something as imported (`MapImported()`) then we return with
the corresponding `To` decl whenever we want to import that node again, this way
circles are handled. In order to make this algorithm work we must ensure
things, which are handled in the generic CreateDecl<> template:
* There are no `Import()` calls in between any node creation (::Create)
and the `MapImported()` call.
* Before actually creating an AST node (::Create), we must check if
the Node had been imported already, if yes then return with that one.
One very important case for this is connected to templates: we may
start an import both from the templated decl of a template and from
the template itself.
Now, the virtual `Imported` function is called in `ASTImporter::Impor(Decl *)`,
but only once, when the `Decl` is imported. One point of this refactor is to
separate responsibilities. The original `Imported()` had 3 responsibilities:
- notify subclasses when an import happened
- register the decl into `ImportedDecls`
- initialise the Decl (set attributes, etc)
Now all of these are in separate functions:
- `Imported`
- `MapImported`
- `InitializeImportedDecl`
I tried to check all the clients, I executed tests for `ExternalASTMerger.cpp`
and some unittests for lldb.
Reviewers: a.sidorin, balazske, xazax.hun, r.stahl
Subscribers: rnkovacs, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D47632
llvm-svn: 336896
2018-07-12 17:42:05 +08:00
|
|
|
Importer.ASTImporter::MapImported(cast<Decl>(Origin.DC), const_cast<Decl*>(cast<Decl>(ToDC)));
|
2017-09-28 03:57:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ExternalASTMerger::ExternalASTMerger(const ImporterTarget &Target,
|
|
|
|
llvm::ArrayRef<ImporterSource> Sources) : LogStream(&llvm::nulls()), Target(Target) {
|
2019-09-30 16:52:16 +08:00
|
|
|
SharedState = std::make_shared<ASTImporterSharedState>(
|
|
|
|
*Target.AST.getTranslationUnitDecl());
|
2017-09-28 03:57:58 +08:00
|
|
|
AddSources(Sources);
|
|
|
|
}
|
|
|
|
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
Decl *ExternalASTMerger::FindOriginalDecl(Decl *D) {
|
|
|
|
assert(&D->getASTContext() == &Target.AST);
|
|
|
|
for (const auto &I : Importers)
|
|
|
|
if (auto Result = I->GetOriginalDecl(D))
|
|
|
|
return Result;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
void ExternalASTMerger::AddSources(llvm::ArrayRef<ImporterSource> Sources) {
|
|
|
|
for (const ImporterSource &S : Sources) {
|
2019-10-01 17:02:05 +08:00
|
|
|
assert(&S.getASTContext() != &Target.AST);
|
[lldb][modern-type-lookup] No longer import temporary declarations into the persistent AST
Summary:
As we figured out in D67803, importing declarations from a temporary ASTContext that were originally from a persistent ASTContext
causes a bunch of duplicated declarations where we end up having declarations in the target AST that have no associated ASTImporter that
can complete them.
I haven't figured out how/if we can solve this in the current way we do things in LLDB, but in the modern-type-lookup this is solvable
as we have a saner architecture with the ExternalASTMerger. As we can (hopefully) make modern-type-lookup the default mode in the future,
I would say we try fixing this issue here. As we don't use the hack that was reinstated in D67803 during modern-type-lookup, the test case for this
is essentially just printing any kind of container in `std::` as we would otherwise run into the issue that required a hack like D67803.
What this patch is doing in essence is that instead of importing a declaration from a temporary ASTContext, we instead check if the
declaration originally came from a persistent ASTContext (e.g. the debug information) and we directly import from there. The ExternalASTMerger
is already connected with ASTImporters to these different sources, so this patch is essentially just two parts:
1. Mark our temporary ASTContext/ImporterSource as temporary when we import from the expression AST.
2. If the ExternalASTMerger sees we import from the expression AST, instead of trying to import these temporary declarations, check if we
can instead import from the persistent ASTContext that is already connected. This ensures that all records from the persistent source actually
come from the persistent source and are minimally imported in a way that allows them to be completed later on in the target AST.
The next step is to run the ASTImporter for these temporary expressions with the MinimalImport mode disabled, but that's a follow up patch.
This patch fixes most test failures with modern-type-lookup enabled by default (down to 73 failing tests, which includes the 22 import-std-module tests
which need special treatment).
Reviewers: shafik, martong
Reviewed By: martong
Subscribers: aprantl, rnkovacs, christof, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D68326
llvm-svn: 373711
2019-10-04 16:26:17 +08:00
|
|
|
// Check that the associated merger actually imports into the source AST.
|
|
|
|
assert(!S.getMerger() || &S.getMerger()->Target.AST == &S.getASTContext());
|
2019-08-15 07:04:18 +08:00
|
|
|
Importers.push_back(std::make_unique<LazyASTImporter>(
|
2019-10-01 17:02:05 +08:00
|
|
|
*this, Target.AST, Target.FM, S, SharedState));
|
2017-09-28 03:57:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::RemoveSources(llvm::ArrayRef<ImporterSource> Sources) {
|
|
|
|
if (LoggingEnabled())
|
|
|
|
for (const ImporterSource &S : Sources)
|
2019-10-01 17:02:05 +08:00
|
|
|
logs() << "(ExternalASTMerger*)" << (void *)this
|
|
|
|
<< " removing source (ASTContext*)" << (void *)&S.getASTContext()
|
2017-09-28 03:57:58 +08:00
|
|
|
<< "\n";
|
|
|
|
Importers.erase(
|
|
|
|
std::remove_if(Importers.begin(), Importers.end(),
|
|
|
|
[&Sources](std::unique_ptr<ASTImporter> &Importer) -> bool {
|
|
|
|
for (const ImporterSource &S : Sources) {
|
2019-10-01 17:02:05 +08:00
|
|
|
if (&Importer->getFromContext() == &S.getASTContext())
|
2017-09-28 03:57:58 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}),
|
|
|
|
Importers.end());
|
|
|
|
for (OriginMap::iterator OI = Origins.begin(), OE = Origins.end(); OI != OE; ) {
|
|
|
|
std::pair<const DeclContext *, DCOrigin> Origin = *OI;
|
|
|
|
bool Erase = false;
|
|
|
|
for (const ImporterSource &S : Sources) {
|
2019-10-01 17:02:05 +08:00
|
|
|
if (&S.getASTContext() == Origin.second.AST) {
|
2017-09-28 03:57:58 +08:00
|
|
|
Erase = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Erase)
|
|
|
|
OI = Origins.erase(OI);
|
|
|
|
else
|
|
|
|
++OI;
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
2017-09-28 03:57:58 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 19:36:54 +08:00
|
|
|
template <typename DeclTy>
|
|
|
|
static bool importSpecializations(DeclTy *D, ASTImporter *Importer) {
|
2019-04-08 21:59:15 +08:00
|
|
|
for (auto *Spec : D->specializations()) {
|
[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 ImportedSpecOrError = Importer->Import(Spec);
|
2019-04-08 21:59:15 +08:00
|
|
|
if (!ImportedSpecOrError) {
|
|
|
|
llvm::consumeError(ImportedSpecOrError.takeError());
|
2018-01-26 19:36:54 +08:00
|
|
|
return true;
|
2019-04-08 21:59:15 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 19:36:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Imports specializations from template declarations that can be specialized.
|
|
|
|
static bool importSpecializationsIfNeeded(Decl *D, ASTImporter *Importer) {
|
|
|
|
if (!isa<TemplateDecl>(D))
|
|
|
|
return false;
|
|
|
|
if (auto *FunctionTD = dyn_cast<FunctionTemplateDecl>(D))
|
|
|
|
return importSpecializations(FunctionTD, Importer);
|
|
|
|
else if (auto *ClassTD = dyn_cast<ClassTemplateDecl>(D))
|
|
|
|
return importSpecializations(ClassTD, Importer);
|
|
|
|
else if (auto *VarTD = dyn_cast<VarTemplateDecl>(D))
|
|
|
|
return importSpecializations(VarTD, Importer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
|
|
|
|
DeclarationName Name) {
|
|
|
|
llvm::SmallVector<NamedDecl *, 1> Decls;
|
|
|
|
llvm::SmallVector<Candidate, 4> Candidates;
|
2017-04-12 03:33:35 +08:00
|
|
|
|
2017-09-28 03:57:58 +08:00
|
|
|
auto FilterFoundDecl = [&Candidates](const Candidate &C) {
|
|
|
|
if (!HasDeclOfSameType(Candidates, C))
|
|
|
|
Candidates.push_back(C);
|
|
|
|
};
|
|
|
|
|
2019-04-08 21:59:15 +08:00
|
|
|
ForEachMatchingDC(DC,
|
|
|
|
[&](ASTImporter &Forward, ASTImporter &Reverse,
|
|
|
|
Source<const DeclContext *> SourceDC) -> bool {
|
[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 FromNameOrErr = Reverse.Import(Name);
|
2019-04-08 21:59:15 +08:00
|
|
|
if (!FromNameOrErr) {
|
|
|
|
llvm::consumeError(FromNameOrErr.takeError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DeclContextLookupResult Result =
|
|
|
|
SourceDC.get()->lookup(*FromNameOrErr);
|
|
|
|
for (NamedDecl *FromD : Result) {
|
|
|
|
FilterFoundDecl(std::make_pair(FromD, &Forward));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2017-09-28 03:57:58 +08:00
|
|
|
|
|
|
|
if (Candidates.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Decls.reserve(Candidates.size());
|
|
|
|
for (const Candidate &C : Candidates) {
|
2018-01-26 19:36:54 +08:00
|
|
|
Decl *LookupRes = C.first.get();
|
|
|
|
ASTImporter *Importer = C.second;
|
[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 NDOrErr = Importer->Import(LookupRes);
|
2019-04-08 21:59:15 +08:00
|
|
|
assert(NDOrErr);
|
|
|
|
(void)static_cast<bool>(NDOrErr);
|
|
|
|
NamedDecl *ND = cast_or_null<NamedDecl>(*NDOrErr);
|
2018-01-26 19:36:54 +08:00
|
|
|
assert(ND);
|
|
|
|
// If we don't import specialization, they are not available via lookup
|
|
|
|
// because the lookup result is imported TemplateDecl and it does not
|
|
|
|
// reference its specializations until they are imported explicitly.
|
|
|
|
bool IsSpecImportFailed =
|
|
|
|
importSpecializationsIfNeeded(LookupRes, Importer);
|
|
|
|
assert(!IsSpecImportFailed);
|
2018-01-26 20:06:44 +08:00
|
|
|
(void)IsSpecImportFailed;
|
2018-01-26 19:36:54 +08:00
|
|
|
Decls.push_back(ND);
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
|
|
|
SetExternalVisibleDeclsForName(DC, Name, Decls);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalASTMerger::FindExternalLexicalDecls(
|
|
|
|
const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
|
|
|
|
SmallVectorImpl<Decl *> &Result) {
|
2017-09-28 03:57:58 +08:00
|
|
|
ForEachMatchingDC(DC, [&](ASTImporter &Forward, ASTImporter &Reverse,
|
|
|
|
Source<const DeclContext *> SourceDC) -> bool {
|
|
|
|
for (const Decl *SourceDecl : SourceDC.get()->decls()) {
|
|
|
|
if (IsKindWeWant(SourceDecl->getKind())) {
|
[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 ImportedDeclOrErr = Forward.Import(SourceDecl);
|
2019-04-08 21:59:15 +08:00
|
|
|
if (ImportedDeclOrErr)
|
|
|
|
assert(!(*ImportedDeclOrErr) ||
|
|
|
|
IsSameDC((*ImportedDeclOrErr)->getDeclContext(), DC));
|
|
|
|
else
|
|
|
|
llvm::consumeError(ImportedDeclOrErr.takeError());
|
2017-09-28 03:57:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2017-04-12 03:33:35 +08:00
|
|
|
}
|
2017-05-13 08:46:33 +08:00
|
|
|
|