22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/iterator_range.h"
25#include "llvm/Support/Capacity.h"
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/ErrorHandling.h"
44 bool InQuotes,
bool ImportedModule,
48 Kind(Kind), ImportedModule(ImportedModule),
File(
File) {
52 this->FileName = StringRef(Memory,
FileName.size());
59llvm::iterator_range<PreprocessingRecord::iterator>
64 if (CachedRangeQuery.Range ==
Range) {
65 return llvm::make_range(
iterator(
this, CachedRangeQuery.Result.first),
66 iterator(
this, CachedRangeQuery.Result.second));
69 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(
Range);
71 CachedRangeQuery.Range =
Range;
72 CachedRangeQuery.Result = Res;
74 return llvm::make_range(
iterator(
this, Res.first),
88 return SM.isInFileID(
SM.getFileLoc(
Loc), FID);
102 int Pos = std::distance(
iterator(
this, 0), PPEI);
104 if (
unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
105 assert(0 &&
"Out-of bounds loaded preprocessed entity");
109 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
115 if (std::optional<bool> IsInFile =
122 getLoadedPreprocessedEntity(LoadedIndex),
126 if (
unsigned(Pos) >= PreprocessedEntities.size()) {
127 assert(0 &&
"Out-of bounds local preprocessed entity");
141 std::pair<unsigned, unsigned>
142 Local = findLocalPreprocessedEntitiesInRange(
Range);
146 return std::make_pair(Local.first, Local.second);
148 std::pair<unsigned, unsigned>
152 if (Loaded.first == Loaded.second)
153 return std::make_pair(Local.first, Local.second);
155 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
158 if (Local.first == Local.second)
159 return std::make_pair(
int(Loaded.first)-TotalLoaded,
160 int(Loaded.second)-TotalLoaded);
163 return std::make_pair(
int(Loaded.first)-TotalLoaded, Local.second);
166std::pair<unsigned, unsigned>
167PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
170 return std::make_pair(0,0);
174 unsigned End = findEndLocalPreprocessedEntity(
Range.
getEnd());
175 return std::make_pair(
Begin, End);
180template <SourceLocation (SourceRange::*getRangeLoc)() const>
189 return SM.isBeforeInTranslationUnit(LHS, RHS);
194 return SM.isBeforeInTranslationUnit(LHS, RHS);
199 return SM.isBeforeInTranslationUnit(LHS, RHS);
204 return (
Range.*getRangeLoc)();
210unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
215 size_t Count = PreprocessedEntities.size();
217 std::vector<PreprocessedEntity *>::const_iterator
218 First = PreprocessedEntities.begin();
219 std::vector<PreprocessedEntity *>::const_iterator I;
228 std::advance(I,
Half);
233 Count = Count -
Half - 1;
238 return First - PreprocessedEntities.begin();
246 auto I = llvm::upper_bound(PreprocessedEntities,
Loc,
247 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
248 return I - PreprocessedEntities.begin();
251PreprocessingRecord::PPEntityID
256 if (isa<MacroDefinitionRecord>(Entity)) {
257 assert((PreprocessedEntities.empty() ||
260 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
261 "a macro definition was encountered out-of-order");
262 PreprocessedEntities.push_back(Entity);
263 return getPPEntityID(PreprocessedEntities.size()-1,
false);
267 if (PreprocessedEntities.empty() ||
269 PreprocessedEntities.back()->getSourceRange().getBegin())) {
270 PreprocessedEntities.push_back(Entity);
271 return getPPEntityID(PreprocessedEntities.size()-1,
false);
286 using pp_iter = std::vector<PreprocessedEntity *>::iterator;
291 for (pp_iter RI = PreprocessedEntities.end(),
292 Begin = PreprocessedEntities.begin();
293 RI !=
Begin && count < 4; --RI, ++count) {
297 (*I)->getSourceRange().getBegin())) {
298 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
299 return getPPEntityID(insertI - PreprocessedEntities.begin(),
306 llvm::upper_bound(PreprocessedEntities, BeginLoc,
307 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
308 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
309 return getPPEntityID(insertI - PreprocessedEntities.begin(),
316 "Preprocessing record already has an external source");
320unsigned PreprocessingRecord::allocateLoadedEntities(
unsigned NumEntities) {
321 unsigned Result = LoadedPreprocessedEntities.size();
322 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
327unsigned PreprocessingRecord::allocateSkippedRanges(
unsigned NumRanges) {
328 unsigned Result = SkippedRanges.size();
329 SkippedRanges.resize(SkippedRanges.size() + NumRanges);
330 SkippedRangesAllLoaded =
false;
334void PreprocessingRecord::ensureSkippedRangesLoaded() {
337 for (
unsigned Index = 0; Index != SkippedRanges.size(); ++Index) {
341 SkippedRangesAllLoaded =
true;
344void PreprocessingRecord::RegisterMacroDefinition(
MacroInfo *Macro,
346 MacroDefinitions[
Macro] = Def;
352 unsigned Index = -PPID.ID - 1;
353 assert(Index < LoadedPreprocessedEntities.size() &&
354 "Out-of bounds loaded preprocessed entity");
355 return getLoadedPreprocessedEntity(Index);
360 unsigned Index = PPID.ID - 1;
361 assert(Index < PreprocessedEntities.size() &&
362 "Out-of bounds local preprocessed entity");
363 return PreprocessedEntities[Index];
368PreprocessingRecord::getLoadedPreprocessedEntity(
unsigned Index) {
369 assert(Index < LoadedPreprocessedEntities.size() &&
370 "Out-of bounds loaded preprocessed entity");
384 return MacroDefinitions.lookup(MI);
387void PreprocessingRecord::addMacroExpansion(
const Token &
Id,
391 if (
Id.getLocation().isMacroID())
426 const Token &MacroNameTok,
434void PreprocessingRecord::Defined(
const Token &MacroNameTok,
449void PreprocessingRecord::MacroExpands(
const Token &
Id,
456void PreprocessingRecord::MacroDefined(
const Token &
Id,
463 MacroDefinitions[MI] = Def;
466void PreprocessingRecord::MacroUndefined(
const Token &
Id,
472void PreprocessingRecord::InclusionDirective(
475 StringRef SearchPath, StringRef RelativePath,
const Module *SuggestedModule,
480 case tok::pp_include:
488 case tok::pp_include_next:
492 case tok::pp___include_macros:
497 llvm_unreachable(
"Unknown include directive kind");
504 EndLoc = FilenameRange.
getEnd();
516 return BumpAlloc.getTotalMemory()
517 + llvm::capacity_in_bytes(MacroDefinitions)
518 + llvm::capacity_in_bytes(PreprocessedEntities)
519 + llvm::capacity_in_bytes(LoadedPreprocessedEntities)
520 + llvm::capacity_in_bytes(SkippedRanges);
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
llvm::MachO::FileType FileType
Defines the clang::MacroInfo and clang::MacroDirective classes.
static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID, SourceManager &SM)
Defines the clang::SourceLocation class and associated facilities.
static bool isInvalid(LocType Loc, bool *Invalid)
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
Represents a character-granular source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
An abstract class that should be subclassed by any external source of preprocessing record entries.
virtual ~ExternalPreprocessingRecordSource()
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
Record the location of an inclusion directive, such as an #include or #import statement.
InclusionKind
The kind of inclusion directives known to the preprocessor.
@ IncludeMacros
A Clang #__include_macros directive.
@ Import
An Objective-C #import directive.
@ IncludeNext
A GNU #include_next directive.
@ Include
An #include directive.
InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind, StringRef FileName, bool InQuotes, bool ImportedModule, OptionalFileEntryRef File, SourceRange Range)
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Record the location of a macro definition.
A description of the current definition of a macro.
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
void forAllDefinitions(Fn F) const
Encapsulates changes to the "macros namespace" (the location where the macro name became active,...
const MacroInfo * getMacroInfo() const
Records the location of a macro expansion.
Encapsulates the data about a macro definition (e.g.
SourceLocation getDefinitionEndLoc() const
Return the location of the last token in the macro.
bool isBuiltinMacro() const
Return true if this macro requires processing before expansion.
SourceLocation getDefinitionLoc() const
Return the location that the macro was defined at.
Describes a module or submodule.
Base class that describes a preprocessed entity, which may be a preprocessor directive or macro expan...
@ InvalidKind
Indicates a problem trying to load the preprocessed entity.
SourceRange getSourceRange() const LLVM_READONLY
Retrieve the source range that covers this entire preprocessed entity.
Records the presence of a preprocessor directive.
Iteration over the preprocessed entities.
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
PreprocessingRecord(SourceManager &SM)
Construct a new preprocessing record.
void * Allocate(unsigned Size, unsigned Align=8)
Allocate memory in the preprocessing record.
llvm::iterator_range< iterator > getPreprocessedEntitiesInRange(SourceRange R)
Returns a range of preprocessed entities that source range R encompasses.
MacroDefinitionRecord * findMacroDefinition(const MacroInfo *MI)
Retrieve the macro definition that corresponds to the given MacroInfo.
PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity)
Add a new preprocessed entity to this record.
bool isEntityInFileID(iterator PPEI, FileID FID)
Returns true if the preprocessed entity that PPEI iterator points to is coming from the file FID.
size_t getTotalMemory() const
std::pair< int, int > Result
void SetExternalSource(ExternalPreprocessingRecordSource &Source)
Set the external source for preprocessed entities.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
bool isLoadedSourceLocation(SourceLocation Loc) const
Returns true if Loc came from a PCH/Module.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Token - This structure provides full information about a lexed token.
IdentifierInfo * getIdentifierInfo() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
The JSON file list parser is used to communicate input to InstallAPI.