blob: b1c0c716c1c42489950252505f636ff16a002d2b [file] [log] [blame]
Douglas Gregor065f8d12010-03-18 17:52:52 +00001//===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PreprocessingRecord class, which maintains a record
11// of what occurred during preprocessing, and its helpers.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/Lex/PreprocessingRecord.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Token.h"
Douglas Gregor796d76a2010-10-20 22:00:55 +000017#include "llvm/Support/ErrorHandling.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000018#include "llvm/Support/Capacity.h"
Douglas Gregor065f8d12010-03-18 17:52:52 +000019
20using namespace clang;
21
Douglas Gregoraae92242010-03-19 21:51:54 +000022ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
23
Douglas Gregorf09b6c92010-11-01 15:03:47 +000024
25InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
26 InclusionKind Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000027 StringRef FileName,
Douglas Gregorf09b6c92010-11-01 15:03:47 +000028 bool InQuotes, const FileEntry *File,
29 SourceRange Range)
30 : PreprocessingDirective(InclusionDirectiveKind, Range),
31 InQuotes(InQuotes), Kind(Kind), File(File)
32{
33 char *Memory
34 = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
35 memcpy(Memory, FileName.data(), FileName.size());
36 Memory[FileName.size()] = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000037 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000038}
39
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000040PreprocessingRecord::PreprocessingRecord(SourceManager &SM,
41 bool IncludeNestedMacroExpansions)
42 : SourceMgr(SM), IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +000043 ExternalSource(0)
Douglas Gregoraae92242010-03-19 21:51:54 +000044{
45}
46
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +000047/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
48/// that source range \arg R encompasses.
49std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
50PreprocessingRecord::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()));
54
55 std::pair<unsigned, unsigned>
56 Local = findLocalPreprocessedEntitiesInRange(Range);
57
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));
62
63 std::pair<unsigned, unsigned>
64 Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range);
65
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));
70
71 unsigned TotalLoaded = LoadedPreprocessedEntities.size();
72
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));
77
78 // Range spands loaded and local entities.
79 return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded),
80 iterator(this, Local.second));
81}
82
83std::pair<unsigned, unsigned>
84PreprocessingRecord::findLocalPreprocessedEntitiesInRange(
85 SourceRange Range) const {
86 if (Range.isInvalid())
87 return std::make_pair(0,0);
88 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
89
90 unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin());
91 unsigned End = findEndLocalPreprocessedEntity(Range.getEnd());
92 return std::make_pair(Begin, End);
93}
94
95namespace {
96
97template <SourceLocation (SourceRange::*getRangeLoc)() const>
98struct PPEntityComp {
99 const SourceManager &SM;
100
101 explicit PPEntityComp(const SourceManager &SM) : SM(SM) { }
102
103 bool operator()(PreprocessedEntity *L, SourceLocation RHS) {
104 SourceLocation LHS = getLoc(L);
105 return SM.isBeforeInTranslationUnit(LHS, RHS);
106 }
107
108 bool operator()(SourceLocation LHS, PreprocessedEntity *R) {
109 SourceLocation RHS = getLoc(R);
110 return SM.isBeforeInTranslationUnit(LHS, RHS);
111 }
112
113 SourceLocation getLoc(PreprocessedEntity *PPE) const {
114 return (PPE->getSourceRange().*getRangeLoc)();
115 }
116};
117
118}
119
120unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity(
121 SourceLocation Loc) const {
122 if (SourceMgr.isLoadedSourceLocation(Loc))
123 return 0;
124
125 std::vector<PreprocessedEntity *>::const_iterator
126 I = std::lower_bound(PreprocessedEntities.begin(),
127 PreprocessedEntities.end(),
128 Loc,
129 PPEntityComp<&SourceRange::getEnd>(SourceMgr));
130 return I - PreprocessedEntities.begin();
131}
132
133unsigned PreprocessingRecord::findEndLocalPreprocessedEntity(
134 SourceLocation Loc) const {
135 if (SourceMgr.isLoadedSourceLocation(Loc))
136 return 0;
137
138 std::vector<PreprocessedEntity *>::const_iterator
139 I = std::upper_bound(PreprocessedEntities.begin(),
140 PreprocessedEntities.end(),
141 Loc,
142 PPEntityComp<&SourceRange::getBegin>(SourceMgr));
143 return I - PreprocessedEntities.begin();
144}
145
Douglas Gregor065f8d12010-03-18 17:52:52 +0000146void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000147 SourceLocation Loc = Entity->getSourceRange().getBegin();
148 assert((PreprocessedEntities.empty() ||
149 !SourceMgr.isBeforeInTranslationUnit(Loc,
150 PreprocessedEntities.back()->getSourceRange().getEnd())) &&
151 "Adding a preprocessed entity that is before the previous one in TU");
Douglas Gregor065f8d12010-03-18 17:52:52 +0000152 PreprocessedEntities.push_back(Entity);
153}
154
Douglas Gregoraae92242010-03-19 21:51:54 +0000155void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000156 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +0000157 assert(!ExternalSource &&
158 "Preprocessing record already has an external source");
159 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +0000160}
161
Douglas Gregor4a9c39a2011-07-21 00:47:40 +0000162unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
163 unsigned Result = LoadedPreprocessedEntities.size();
164 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
165 + NumEntities);
166 return Result;
167}
168
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000169void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
170 PPEntityID PPID) {
171 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +0000172}
173
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000174/// \brief Retrieve the preprocessed entity at the given ID.
175PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
176 if (PPID < 0) {
177 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
178 "Out-of bounds loaded preprocessed entity");
179 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
180 }
181 assert(unsigned(PPID) < PreprocessedEntities.size() &&
182 "Out-of bounds local preprocessed entity");
183 return PreprocessedEntities[PPID];
184}
185
186/// \brief Retrieve the loaded preprocessed entity at the given index.
187PreprocessedEntity *
188PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
189 assert(Index < LoadedPreprocessedEntities.size() &&
190 "Out-of bounds loaded preprocessed entity");
191 assert(ExternalSource && "No external source to load from");
192 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
193 if (!Entity) {
194 Entity = ExternalSource->ReadPreprocessedEntity(Index);
195 if (!Entity) // Failed to load.
196 Entity = new (*this)
197 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
198 }
199 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000200}
201
Douglas Gregor8aaca672010-03-19 21:58:23 +0000202MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000203 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000204 = MacroDefinitions.find(MI);
205 if (Pos == MacroDefinitions.end())
206 return 0;
207
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000208 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
209 if (Entity->isInvalid())
210 return 0;
211 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000212}
213
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000214void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
215 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000216 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000217 return;
218
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000219 if (MI->isBuiltinMacro())
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000220 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000221 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
222 else if (MacroDefinition *Def = findMacroDefinition(MI))
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000223 addPreprocessedEntity(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000224 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000225}
226
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000227void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000228 const MacroInfo *MI) {
229 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
230 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000231 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
232 MI->getDefinitionLoc(),
233 R);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000234 addPreprocessedEntity(Def);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000235 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
236 /*isLoaded=*/false);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000237}
Douglas Gregoraae92242010-03-19 21:51:54 +0000238
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000239void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000240 const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000241 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor8aaca672010-03-19 21:58:23 +0000242 = MacroDefinitions.find(MI);
243 if (Pos != MacroDefinitions.end())
244 MacroDefinitions.erase(Pos);
245}
246
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000247void PreprocessingRecord::InclusionDirective(
248 SourceLocation HashLoc,
249 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000250 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000251 bool IsAngled,
252 const FileEntry *File,
253 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000254 StringRef SearchPath,
255 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000256 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
257
258 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
259 case tok::pp_include:
260 Kind = InclusionDirective::Include;
261 break;
262
263 case tok::pp_import:
264 Kind = InclusionDirective::Import;
265 break;
266
267 case tok::pp_include_next:
268 Kind = InclusionDirective::IncludeNext;
269 break;
270
271 case tok::pp___include_macros:
272 Kind = InclusionDirective::IncludeMacros;
273 break;
274
275 default:
276 llvm_unreachable("Unknown include directive kind");
277 return;
278 }
279
280 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000281 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
282 File, SourceRange(HashLoc, EndLoc));
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +0000283 addPreprocessedEntity(ID);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000284}
Ted Kremenek182543a2011-07-26 21:17:24 +0000285
286size_t PreprocessingRecord::getTotalMemory() const {
287 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000288 + llvm::capacity_in_bytes(MacroDefinitions)
289 + llvm::capacity_in_bytes(PreprocessedEntities)
290 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000291}