llvm-project/clang/lib/AST/ExternalASTSource.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.8 KiB
C++
Raw Normal View History

//===- ExternalASTSource.cpp - Abstract External AST Interface ------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file provides the default implementation of the ExternalASTSource
// interface, which enables construction of AST nodes from some external
// source.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ExternalASTSource.h"
If a declaration is loaded, and then a module import adds a redeclaration, then ensure that querying the first declaration for its most recent declaration checks for redeclarations from the imported module. This works as follows: * The 'most recent' pointer on a canonical declaration grows a pointer to the external AST source and a generation number (space- and time-optimized for the case where there is no external source). * Each time the 'most recent' pointer is queried, if it has an external source, we check whether it's up to date, and update it if not. * The ancillary data stored on the canonical declaration is allocated lazily to avoid filling it in for declarations that end up being non-canonical. We'll still perform a redundant (ASTContext) allocation if someone asks for the most recent declaration from a decl before setPreviousDecl is called, but such cases are probably all bugs, and are now easy to find. Some finessing is still in order here -- in particular, we use a very general mechanism for handling the DefinitionData pointer on CXXRecordData, and a more targeted approach would be more compact. Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was addressing only a corner of the full problem space here. That's not covered by this patch. Early performance benchmarks show that this makes no measurable difference to Clang performance without modules enabled (and fixes a major correctness issue with modules enabled). I'll revert if a full performance comparison shows any problems. llvm-svn: 209046
2014-05-17 07:01:30 +08:00
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclarationName.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/Module.h"
Avoid SourceManager.h include in RawCommentList.h, add missing incs SourceManager.h includes FileManager.h, which is expensive due to dependencies on LLVM FS headers. Remove dead BeforeThanCompare specialization. Sink ASTContext::addComment to cpp file. This reduces the time to compile a file that does nothing but include ASTContext.h from ~3.4s to ~2.8s for me. Saves these includes: 219 - ../clang/include/clang/Basic/SourceManager.h 204 - ../clang/include/clang/Basic/FileSystemOptions.h 204 - ../clang/include/clang/Basic/FileManager.h 165 - ../llvm/include/llvm/Support/VirtualFileSystem.h 164 - ../llvm/include/llvm/Support/SourceMgr.h 164 - ../llvm/include/llvm/Support/SMLoc.h 161 - ../llvm/include/llvm/Support/Path.h 141 - ../llvm/include/llvm/ADT/BitVector.h 128 - ../llvm/include/llvm/Support/MemoryBuffer.h 124 - ../llvm/include/llvm/Support/FileSystem.h 124 - ../llvm/include/llvm/Support/Chrono.h 124 - .../MSVCSTL/include/stack 122 - ../llvm/include/llvm-c/Types.h 122 - ../llvm/include/llvm/Support/NativeFormatting.h 122 - ../llvm/include/llvm/Support/FormatProviders.h 122 - ../llvm/include/llvm/Support/CBindingWrapping.h 122 - .../MSVCSTL/include/xtimec.h 122 - .../MSVCSTL/include/ratio 122 - .../MSVCSTL/include/chrono 121 - ../llvm/include/llvm/Support/FormatVariadicDetails.h 118 - ../llvm/include/llvm/Support/MD5.h 109 - .../MSVCSTL/include/deque 105 - ../llvm/include/llvm/Support/Host.h 105 - ../llvm/include/llvm/Support/Endian.h Reviewed By: aaron.ballman, hans Differential Revision: https://reviews.llvm.org/D75279
2020-02-28 03:01:58 +08:00
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/None.h"
If a declaration is loaded, and then a module import adds a redeclaration, then ensure that querying the first declaration for its most recent declaration checks for redeclarations from the imported module. This works as follows: * The 'most recent' pointer on a canonical declaration grows a pointer to the external AST source and a generation number (space- and time-optimized for the case where there is no external source). * Each time the 'most recent' pointer is queried, if it has an external source, we check whether it's up to date, and update it if not. * The ancillary data stored on the canonical declaration is allocated lazily to avoid filling it in for declarations that end up being non-canonical. We'll still perform a redundant (ASTContext) allocation if someone asks for the most recent declaration from a decl before setPreviousDecl is called, but such cases are probably all bugs, and are now easy to find. Some finessing is still in order here -- in particular, we use a very general mechanism for handling the DefinitionData pointer on CXXRecordData, and a more targeted approach would be more compact. Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was addressing only a corner of the full problem space here. That's not covered by this patch. Early performance benchmarks show that this makes no measurable difference to Clang performance without modules enabled (and fixes a major correctness issue with modules enabled). I'll revert if a full performance comparison shows any problems. llvm-svn: 209046
2014-05-17 07:01:30 +08:00
#include "llvm/Support/ErrorHandling.h"
#include <cstdint>
using namespace clang;
char ExternalASTSource::ID;
ExternalASTSource::~ExternalASTSource() = default;
llvm::Optional<ASTSourceDescriptor>
ExternalASTSource::getSourceDescriptor(unsigned ID) {
return None;
}
ExternalASTSource::ExtKind
ExternalASTSource::hasExternalDefinitions(const Decl *D) {
return EK_ReplyHazy;
}
void ExternalASTSource::FindFileRegionDecls(FileID File, unsigned Offset,
unsigned Length,
SmallVectorImpl<Decl *> &Decls) {}
void ExternalASTSource::CompleteRedeclChain(const Decl *D) {}
void ExternalASTSource::CompleteType(TagDecl *Tag) {}
void ExternalASTSource::CompleteType(ObjCInterfaceDecl *Class) {}
void ExternalASTSource::ReadComments() {}
void ExternalASTSource::StartedDeserializing() {}
void ExternalASTSource::FinishedDeserializing() {}
void ExternalASTSource::StartTranslationUnit(ASTConsumer *Consumer) {}
void ExternalASTSource::PrintStats() {}
bool ExternalASTSource::layoutRecordType(
const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment,
llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets) {
return false;
}
Decl *ExternalASTSource::GetExternalDecl(uint32_t ID) {
return nullptr;
}
Selector ExternalASTSource::GetExternalSelector(uint32_t ID) {
return Selector();
}
uint32_t ExternalASTSource::GetNumExternalSelectors() {
return 0;
}
Stmt *ExternalASTSource::GetExternalDeclStmt(uint64_t Offset) {
return nullptr;
}
CXXCtorInitializer **
ExternalASTSource::GetExternalCXXCtorInitializers(uint64_t Offset) {
return nullptr;
}
CXXBaseSpecifier *
ExternalASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
return nullptr;
}
bool
ExternalASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
return false;
}
void ExternalASTSource::completeVisibleDeclsMap(const DeclContext *DC) {}
void ExternalASTSource::FindExternalLexicalDecls(
const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
SmallVectorImpl<Decl *> &Result) {}
void ExternalASTSource::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {}
If a declaration is loaded, and then a module import adds a redeclaration, then ensure that querying the first declaration for its most recent declaration checks for redeclarations from the imported module. This works as follows: * The 'most recent' pointer on a canonical declaration grows a pointer to the external AST source and a generation number (space- and time-optimized for the case where there is no external source). * Each time the 'most recent' pointer is queried, if it has an external source, we check whether it's up to date, and update it if not. * The ancillary data stored on the canonical declaration is allocated lazily to avoid filling it in for declarations that end up being non-canonical. We'll still perform a redundant (ASTContext) allocation if someone asks for the most recent declaration from a decl before setPreviousDecl is called, but such cases are probably all bugs, and are now easy to find. Some finessing is still in order here -- in particular, we use a very general mechanism for handling the DefinitionData pointer on CXXRecordData, and a more targeted approach would be more compact. Also, the MayHaveOutOfDateDef mechanism should now be expunged, since it was addressing only a corner of the full problem space here. That's not covered by this patch. Early performance benchmarks show that this makes no measurable difference to Clang performance without modules enabled (and fixes a major correctness issue with modules enabled). I'll revert if a full performance comparison shows any problems. llvm-svn: 209046
2014-05-17 07:01:30 +08:00
uint32_t ExternalASTSource::incrementGeneration(ASTContext &C) {
uint32_t OldGeneration = CurrentGeneration;
// Make sure the generation of the topmost external source for the context is
// incremented. That might not be us.
auto *P = C.getExternalSource();
if (P && P != this)
CurrentGeneration = P->incrementGeneration(C);
else {
// FIXME: Only bump the generation counter if the current generation number
// has been observed?
if (!++CurrentGeneration)
llvm::report_fatal_error("generation counter overflowed", false);
}
return OldGeneration;
}