1 //===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PreprocessingRecord class, which maintains a record
11 // of what occurred during preprocessing, and its helpers.
13 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/PreprocessingRecord.h"
15 #include "clang/Lex/MacroInfo.h"
16 #include "clang/Lex/Token.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/Capacity.h"
20 using namespace clang;
22 ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
25 InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
28 bool InQuotes, const FileEntry *File,
30 : PreprocessingDirective(InclusionDirectiveKind, Range),
31 InQuotes(InQuotes), Kind(Kind), File(File)
34 = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
35 memcpy(Memory, FileName.data(), FileName.size());
36 Memory[FileName.size()] = 0;
37 this->FileName = StringRef(Memory, FileName.size());
40 PreprocessingRecord::PreprocessingRecord(SourceManager &SM,
41 bool IncludeNestedMacroExpansions)
42 : SourceMgr(SM), IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
47 /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
48 /// that source range \arg R encompasses.
49 std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
50 PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) {
51 if (Range.isInvalid())
52 return std::make_pair(iterator(this, 0), iterator(this, 0));
53 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
55 std::pair<unsigned, unsigned>
56 Local = findLocalPreprocessedEntitiesInRange(Range);
58 // Check if range spans local entities.
59 if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin()))
60 return std::make_pair(iterator(this, Local.first),
61 iterator(this, Local.second));
63 std::pair<unsigned, unsigned>
64 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
66 // Check if range spans local entities.
67 if (Loaded.first == Loaded.second)
68 return std::make_pair(iterator(this, Local.first),
69 iterator(this, Local.second));
71 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
73 // Check if range spans loaded entities.
74 if (Local.first == Local.second)
75 return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded),
76 iterator(this, int(Loaded.second)-TotalLoaded));
78 // Range spands loaded and local entities.
79 return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded),
80 iterator(this, Local.second));
83 std::pair<unsigned, unsigned>
84 PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
85 SourceRange Range) const {
86 if (Range.isInvalid())
87 return std::make_pair(0,0);
88 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
90 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
91 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
92 return std::make_pair(Begin, End);
97 template <SourceLocation (SourceRange::*getRangeLoc)() const>
99 const SourceManager &SM;
101 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
103 bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const {
104 SourceLocation LHS = getLoc(L);
105 SourceLocation RHS = getLoc(R);
106 return SM.isBeforeInTranslationUnit(LHS, RHS);
109 bool operator()(PreprocessedEntity *L, SourceLocation RHS) const {
110 SourceLocation LHS = getLoc(L);
111 return SM.isBeforeInTranslationUnit(LHS, RHS);
114 bool operator()(SourceLocation LHS, PreprocessedEntity *R) const {
115 SourceLocation RHS = getLoc(R);
116 return SM.isBeforeInTranslationUnit(LHS, RHS);
119 SourceLocation getLoc(PreprocessedEntity *PPE) const {
120 SourceRange Range = PPE->getSourceRange();
121 return (Range.*getRangeLoc)();
127 unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
128 SourceLocation Loc) const {
129 if (SourceMgr.isLoadedSourceLocation(Loc))
132 size_t Count = PreprocessedEntities.size();
134 std::vector<PreprocessedEntity *>::const_iterator
135 First = PreprocessedEntities.begin();
136 std::vector<PreprocessedEntity *>::const_iterator I;
138 // Do a binary search manually instead of using std::lower_bound because
139 // The end locations of entities may be unordered (when a macro expansion
140 // is inside another macro argument), but for this case it is not important
141 // whether we get the first macro expansion or its containing macro.
145 std::advance(I, Half);
146 if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(),
150 Count = Count - Half - 1;
155 return First - PreprocessedEntities.begin();
158 unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
159 SourceLocation Loc) const {
160 if (SourceMgr.isLoadedSourceLocation(Loc))
163 std::vector<PreprocessedEntity *>::const_iterator
164 I = std::upper_bound(PreprocessedEntities.begin(),
165 PreprocessedEntities.end(),
167 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
168 return I - PreprocessedEntities.begin();
171 void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
173 SourceLocation BeginLoc = Entity->getSourceRange().getBegin();
175 // Check normal case, this entity begin location is after the previous one.
176 if (PreprocessedEntities.empty() ||
177 !SourceMgr.isBeforeInTranslationUnit(BeginLoc,
178 PreprocessedEntities.back()->getSourceRange().getBegin())) {
179 PreprocessedEntities.push_back(Entity);
183 // The entity's location is not after the previous one; this can happen rarely
184 // e.g. with "#include MACRO".
185 // Iterate the entities vector in reverse until we find the right place to
186 // insert the new entity.
187 for (std::vector<PreprocessedEntity *>::iterator
188 RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin();
190 std::vector<PreprocessedEntity *>::iterator I = RI;
192 if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc,
193 (*I)->getSourceRange().getBegin())) {
194 PreprocessedEntities.insert(RI, Entity);
200 void PreprocessingRecord::SetExternalSource(
201 ExternalPreprocessingRecordSource &Source) {
202 assert(!ExternalSource &&
203 "Preprocessing record already has an external source");
204 ExternalSource = &Source;
207 unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
208 unsigned Result = LoadedPreprocessedEntities.size();
209 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
214 void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
216 MacroDefinitions[Macro] = PPID;
219 /// \brief Retrieve the preprocessed entity at the given ID.
220 PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
222 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
223 "Out-of bounds loaded preprocessed entity");
224 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
226 assert(unsigned(PPID) < PreprocessedEntities.size() &&
227 "Out-of bounds local preprocessed entity");
228 return PreprocessedEntities[PPID];
231 /// \brief Retrieve the loaded preprocessed entity at the given index.
233 PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
234 assert(Index < LoadedPreprocessedEntities.size() &&
235 "Out-of bounds loaded preprocessed entity");
236 assert(ExternalSource && "No external source to load from");
237 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
239 Entity = ExternalSource->ReadPreprocessedEntity(Index);
240 if (!Entity) // Failed to load.
242 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
247 MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
248 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
249 = MacroDefinitions.find(MI);
250 if (Pos == MacroDefinitions.end())
253 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
254 if (Entity->isInvalid())
256 return cast<MacroDefinition>(Entity);
259 void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
261 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
264 if (MI->isBuiltinMacro())
265 addPreprocessedEntity(
266 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
267 else if (MacroDefinition *Def = findMacroDefinition(MI))
268 addPreprocessedEntity(
269 new (*this) MacroExpansion(Def, Range));
272 void PreprocessingRecord::MacroDefined(const Token &Id,
273 const MacroInfo *MI) {
274 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
276 = new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
277 addPreprocessedEntity(Def);
278 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
282 void PreprocessingRecord::MacroUndefined(const Token &Id,
283 const MacroInfo *MI) {
284 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
285 = MacroDefinitions.find(MI);
286 if (Pos != MacroDefinitions.end())
287 MacroDefinitions.erase(Pos);
290 void PreprocessingRecord::InclusionDirective(
291 SourceLocation HashLoc,
292 const clang::Token &IncludeTok,
295 const FileEntry *File,
296 clang::SourceLocation EndLoc,
297 StringRef SearchPath,
298 StringRef RelativePath) {
299 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
301 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
302 case tok::pp_include:
303 Kind = InclusionDirective::Include;
307 Kind = InclusionDirective::Import;
310 case tok::pp_include_next:
311 Kind = InclusionDirective::IncludeNext;
314 case tok::pp___include_macros:
315 Kind = InclusionDirective::IncludeMacros;
319 llvm_unreachable("Unknown include directive kind");
323 clang::InclusionDirective *ID
324 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
325 File, SourceRange(HashLoc, EndLoc));
326 addPreprocessedEntity(ID);
329 size_t PreprocessingRecord::getTotalMemory() const {
330 return BumpAlloc.getTotalMemory()
331 + llvm::capacity_in_bytes(MacroDefinitions)
332 + llvm::capacity_in_bytes(PreprocessedEntities)
333 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);