22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/iterator_range.h"
24#include "llvm/Support/Capacity.h"
25#include "llvm/Support/ErrorHandling.h"
41 bool InQuotes,
bool ImportedModule,
45 Kind(Kind), ImportedModule(ImportedModule),
File(
File) {
49 this->FileName = StringRef(Memory,
FileName.size());
56llvm::iterator_range<PreprocessingRecord::iterator>
61 if (CachedRangeQuery.Range ==
Range) {
62 return llvm::make_range(
iterator(
this, CachedRangeQuery.Result.first),
63 iterator(
this, CachedRangeQuery.Result.second));
66 std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(
Range);
68 CachedRangeQuery.Range =
Range;
69 CachedRangeQuery.Result = Res;
71 return llvm::make_range(
iterator(
this, Res.first),
85 return SM.isInFileID(
SM.getFileLoc(
Loc), FID);
99 int Pos = std::distance(
iterator(
this, 0), PPEI);
101 if (
unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) {
102 assert(0 &&
"Out-of bounds loaded preprocessed entity");
106 unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos;
112 if (std::optional<bool> IsInFile =
119 getLoadedPreprocessedEntity(LoadedIndex),
123 if (
unsigned(Pos) >= PreprocessedEntities.size()) {
124 assert(0 &&
"Out-of bounds local preprocessed entity");
138 std::pair<unsigned, unsigned>
139 Local = findLocalPreprocessedEntitiesInRange(
Range);
143 return std::make_pair(Local.first, Local.second);
145 std::pair<unsigned, unsigned>
149 if (Loaded.first == Loaded.second)
150 return std::make_pair(Local.first, Local.second);
152 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
155 if (Local.first == Local.second)
156 return std::make_pair(
int(Loaded.first)-TotalLoaded,
157 int(Loaded.second)-TotalLoaded);
160 return std::make_pair(
int(Loaded.first)-TotalLoaded, Local.second);
163std::pair<unsigned, unsigned>
164PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
167 return std::make_pair(0,0);
171 unsigned End = findEndLocalPreprocessedEntity(
Range.
getEnd());
172 return std::make_pair(
Begin, End);
177template <SourceLocation (SourceRange::*getRangeLoc)() const>
186 return SM.isBeforeInTranslationUnit(LHS, RHS);
191 return SM.isBeforeInTranslationUnit(LHS, RHS);
196 return SM.isBeforeInTranslationUnit(LHS, RHS);
201 return (
Range.*getRangeLoc)();
207unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
212 size_t Count = PreprocessedEntities.size();
214 std::vector<PreprocessedEntity *>::const_iterator
215 First = PreprocessedEntities.begin();
216 std::vector<PreprocessedEntity *>::const_iterator I;
225 std::advance(I,
Half);
230 Count = Count -
Half - 1;
235 return First - PreprocessedEntities.begin();
243 auto I = llvm::upper_bound(PreprocessedEntities,
Loc,
244 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
245 return I - PreprocessedEntities.begin();
248PreprocessingRecord::PPEntityID
253 if (isa<MacroDefinitionRecord>(Entity)) {
254 assert((PreprocessedEntities.empty() ||
257 PreprocessedEntities.back()->getSourceRange().getBegin())) &&
258 "a macro definition was encountered out-of-order");
259 PreprocessedEntities.push_back(Entity);
260 return getPPEntityID(PreprocessedEntities.size()-1,
false);
264 if (PreprocessedEntities.empty() ||
266 PreprocessedEntities.back()->getSourceRange().getBegin())) {
267 PreprocessedEntities.push_back(Entity);
268 return getPPEntityID(PreprocessedEntities.size()-1,
false);
283 using pp_iter = std::vector<PreprocessedEntity *>::iterator;
288 for (pp_iter RI = PreprocessedEntities.end(),
289 Begin = PreprocessedEntities.begin();
290 RI !=
Begin && count < 4; --RI, ++count) {
294 (*I)->getSourceRange().getBegin())) {
295 pp_iter insertI = PreprocessedEntities.insert(RI, Entity);
296 return getPPEntityID(insertI - PreprocessedEntities.begin(),
303 llvm::upper_bound(PreprocessedEntities, BeginLoc,
304 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
305 pp_iter insertI = PreprocessedEntities.insert(I, Entity);
306 return getPPEntityID(insertI - PreprocessedEntities.begin(),
313 "Preprocessing record already has an external source");
317unsigned PreprocessingRecord::allocateLoadedEntities(
unsigned NumEntities) {
318 unsigned Result = LoadedPreprocessedEntities.size();
319 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
324unsigned PreprocessingRecord::allocateSkippedRanges(
unsigned NumRanges) {
325 unsigned Result = SkippedRanges.size();
326 SkippedRanges.resize(SkippedRanges.size() + NumRanges);
327 SkippedRangesAllLoaded =
false;
331void PreprocessingRecord::ensureSkippedRangesLoaded() {
334 for (
unsigned Index = 0; Index != SkippedRanges.size(); ++Index) {
338 SkippedRangesAllLoaded =
true;
341void PreprocessingRecord::RegisterMacroDefinition(
MacroInfo *Macro,
343 MacroDefinitions[
Macro] = Def;
349 unsigned Index = -PPID.ID - 1;
350 assert(Index < LoadedPreprocessedEntities.size() &&
351 "Out-of bounds loaded preprocessed entity");
352 return getLoadedPreprocessedEntity(Index);
357 unsigned Index = PPID.ID - 1;
358 assert(Index < PreprocessedEntities.size() &&
359 "Out-of bounds local preprocessed entity");
360 return PreprocessedEntities[Index];
365PreprocessingRecord::getLoadedPreprocessedEntity(
unsigned Index) {
366 assert(Index < LoadedPreprocessedEntities.size() &&
367 "Out-of bounds loaded preprocessed entity");
381 return MacroDefinitions.lookup(MI);
384void PreprocessingRecord::addMacroExpansion(
const Token &
Id,
388 if (
Id.getLocation().isMacroID())
423 const Token &MacroNameTok,
431void PreprocessingRecord::Defined(
const Token &MacroNameTok,
446void PreprocessingRecord::MacroExpands(
const Token &
Id,
453void PreprocessingRecord::MacroDefined(
const Token &
Id,
460 MacroDefinitions[MI] = Def;
463void PreprocessingRecord::MacroUndefined(
const Token &
Id,
469void PreprocessingRecord::InclusionDirective(
472 StringRef SearchPath, StringRef RelativePath,
const Module *SuggestedModule,
477 case tok::pp_include:
485 case tok::pp_include_next:
489 case tok::pp___include_macros:
494 llvm_unreachable(
"Unknown include directive kind");
501 EndLoc = FilenameRange.
getEnd();
513 return BumpAlloc.getTotalMemory()
514 + llvm::capacity_in_bytes(MacroDefinitions)
515 + llvm::capacity_in_bytes(PreprocessedEntities)
516 + llvm::capacity_in_bytes(LoadedPreprocessedEntities)
517 + 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.