Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 1 | //===--- 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 Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Capacity.h" |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 22 | ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { } |
| 23 | |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 24 | |
| 25 | InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, |
| 26 | InclusionKind Kind, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 27 | StringRef FileName, |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 28 | 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 Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | this->FileName = StringRef(Memory, FileName.size()); |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Argyrios Kyrtzidis | 335c5a4 | 2012-02-25 02:41:16 +0000 | [diff] [blame^] | 40 | PreprocessingRecord::PreprocessingRecord(SourceManager &SM) |
| 41 | : SourceMgr(SM), ExternalSource(0) |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 45 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
| 46 | /// that source range \arg R encompasses. |
| 47 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 48 | PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) { |
| 49 | if (Range.isInvalid()) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 50 | return std::make_pair(iterator(), iterator()); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 51 | |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 52 | if (CachedRangeQuery.Range == Range) { |
| 53 | return std::make_pair(iterator(this, CachedRangeQuery.Result.first), |
| 54 | iterator(this, CachedRangeQuery.Result.second)); |
| 55 | } |
| 56 | |
| 57 | std::pair<PPEntityID, PPEntityID> |
| 58 | Res = getPreprocessedEntitiesInRangeSlow(Range); |
| 59 | |
| 60 | CachedRangeQuery.Range = Range; |
| 61 | CachedRangeQuery.Result = Res; |
| 62 | |
| 63 | return std::make_pair(iterator(this, Res.first), iterator(this, Res.second)); |
| 64 | } |
| 65 | |
| 66 | static bool isPreprocessedEntityIfInFileID(PreprocessedEntity *PPE, FileID FID, |
| 67 | SourceManager &SM) { |
| 68 | assert(!FID.isInvalid()); |
| 69 | if (!PPE) |
| 70 | return false; |
| 71 | |
| 72 | SourceLocation Loc = PPE->getSourceRange().getBegin(); |
| 73 | if (Loc.isInvalid()) |
| 74 | return false; |
| 75 | |
| 76 | if (SM.isInFileID(SM.getFileLoc(Loc), FID)) |
| 77 | return true; |
| 78 | else |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /// \brief Returns true if the preprocessed entity that \arg PPEI iterator |
| 83 | /// points to is coming from the file \arg FID. |
| 84 | /// |
| 85 | /// Can be used to avoid implicit deserializations of preallocated |
| 86 | /// preprocessed entities if we only care about entities of a specific file |
| 87 | /// and not from files #included in the range given at |
| 88 | /// \see getPreprocessedEntitiesInRange. |
| 89 | bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) { |
| 90 | if (FID.isInvalid()) |
| 91 | return false; |
| 92 | |
| 93 | PPEntityID PPID = PPEI.Position; |
| 94 | if (PPID < 0) { |
| 95 | assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() && |
| 96 | "Out-of bounds loaded preprocessed entity"); |
| 97 | assert(ExternalSource && "No external source to load from"); |
| 98 | unsigned LoadedIndex = LoadedPreprocessedEntities.size()+PPID; |
| 99 | if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex]) |
| 100 | return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr); |
| 101 | |
| 102 | // See if the external source can see if the entity is in the file without |
| 103 | // deserializing it. |
| 104 | llvm::Optional<bool> |
| 105 | IsInFile = ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID); |
| 106 | if (IsInFile.hasValue()) |
| 107 | return IsInFile.getValue(); |
| 108 | |
| 109 | // The external source did not provide a definite answer, go and deserialize |
| 110 | // the entity to check it. |
| 111 | return isPreprocessedEntityIfInFileID( |
| 112 | getLoadedPreprocessedEntity(LoadedIndex), |
| 113 | FID, SourceMgr); |
| 114 | } |
| 115 | |
| 116 | assert(unsigned(PPID) < PreprocessedEntities.size() && |
| 117 | "Out-of bounds local preprocessed entity"); |
| 118 | return isPreprocessedEntityIfInFileID(PreprocessedEntities[PPID], |
| 119 | FID, SourceMgr); |
| 120 | } |
| 121 | |
| 122 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
| 123 | /// that source range \arg R encompasses. |
| 124 | std::pair<PreprocessingRecord::PPEntityID, PreprocessingRecord::PPEntityID> |
| 125 | PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) { |
| 126 | assert(Range.isValid()); |
| 127 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 128 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 129 | std::pair<unsigned, unsigned> |
| 130 | Local = findLocalPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 131 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 132 | // Check if range spans local entities. |
| 133 | if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin())) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 134 | return std::make_pair(Local.first, Local.second); |
| 135 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 136 | std::pair<unsigned, unsigned> |
| 137 | Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 138 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 139 | // Check if range spans local entities. |
| 140 | if (Loaded.first == Loaded.second) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 141 | return std::make_pair(Local.first, Local.second); |
| 142 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 143 | unsigned TotalLoaded = LoadedPreprocessedEntities.size(); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 144 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 145 | // Check if range spans loaded entities. |
| 146 | if (Local.first == Local.second) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 147 | return std::make_pair(int(Loaded.first)-TotalLoaded, |
| 148 | int(Loaded.second)-TotalLoaded); |
| 149 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 150 | // Range spands loaded and local entities. |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 151 | return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | std::pair<unsigned, unsigned> |
| 155 | PreprocessingRecord::findLocalPreprocessedEntitiesInRange( |
| 156 | SourceRange Range) const { |
| 157 | if (Range.isInvalid()) |
| 158 | return std::make_pair(0,0); |
| 159 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 160 | |
| 161 | unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin()); |
| 162 | unsigned End = findEndLocalPreprocessedEntity(Range.getEnd()); |
| 163 | return std::make_pair(Begin, End); |
| 164 | } |
| 165 | |
| 166 | namespace { |
| 167 | |
| 168 | template <SourceLocation (SourceRange::*getRangeLoc)() const> |
| 169 | struct PPEntityComp { |
| 170 | const SourceManager &SM; |
| 171 | |
| 172 | explicit PPEntityComp(const SourceManager &SM) : SM(SM) { } |
| 173 | |
Benjamin Kramer | 2e9d9cf | 2011-09-21 16:58:20 +0000 | [diff] [blame] | 174 | bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const { |
| 175 | SourceLocation LHS = getLoc(L); |
| 176 | SourceLocation RHS = getLoc(R); |
| 177 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 178 | } |
| 179 | |
| 180 | bool operator()(PreprocessedEntity *L, SourceLocation RHS) const { |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 181 | SourceLocation LHS = getLoc(L); |
| 182 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 183 | } |
| 184 | |
Benjamin Kramer | 2e9d9cf | 2011-09-21 16:58:20 +0000 | [diff] [blame] | 185 | bool operator()(SourceLocation LHS, PreprocessedEntity *R) const { |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 186 | SourceLocation RHS = getLoc(R); |
| 187 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 188 | } |
| 189 | |
| 190 | SourceLocation getLoc(PreprocessedEntity *PPE) const { |
Argyrios Kyrtzidis | a35c444 | 2011-09-19 22:02:08 +0000 | [diff] [blame] | 191 | SourceRange Range = PPE->getSourceRange(); |
| 192 | return (Range.*getRangeLoc)(); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 193 | } |
| 194 | }; |
| 195 | |
| 196 | } |
| 197 | |
| 198 | unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity( |
| 199 | SourceLocation Loc) const { |
| 200 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 201 | return 0; |
| 202 | |
Argyrios Kyrtzidis | e523e38 | 2011-09-22 21:17:02 +0000 | [diff] [blame] | 203 | size_t Count = PreprocessedEntities.size(); |
| 204 | size_t Half; |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 205 | std::vector<PreprocessedEntity *>::const_iterator |
Argyrios Kyrtzidis | e523e38 | 2011-09-22 21:17:02 +0000 | [diff] [blame] | 206 | First = PreprocessedEntities.begin(); |
| 207 | std::vector<PreprocessedEntity *>::const_iterator I; |
| 208 | |
| 209 | // Do a binary search manually instead of using std::lower_bound because |
| 210 | // The end locations of entities may be unordered (when a macro expansion |
| 211 | // is inside another macro argument), but for this case it is not important |
| 212 | // whether we get the first macro expansion or its containing macro. |
| 213 | while (Count > 0) { |
| 214 | Half = Count/2; |
| 215 | I = First; |
| 216 | std::advance(I, Half); |
| 217 | if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(), |
| 218 | Loc)){ |
| 219 | First = I; |
| 220 | ++First; |
| 221 | Count = Count - Half - 1; |
| 222 | } else |
| 223 | Count = Half; |
| 224 | } |
| 225 | |
| 226 | return First - PreprocessedEntities.begin(); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | unsigned PreprocessingRecord::findEndLocalPreprocessedEntity( |
| 230 | SourceLocation Loc) const { |
| 231 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 232 | return 0; |
| 233 | |
| 234 | std::vector<PreprocessedEntity *>::const_iterator |
| 235 | I = std::upper_bound(PreprocessedEntities.begin(), |
| 236 | PreprocessedEntities.end(), |
| 237 | Loc, |
| 238 | PPEntityComp<&SourceRange::getBegin>(SourceMgr)); |
| 239 | return I - PreprocessedEntities.begin(); |
| 240 | } |
| 241 | |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 242 | void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { |
Argyrios Kyrtzidis | 45e8cf5 | 2011-09-20 23:27:33 +0000 | [diff] [blame] | 243 | assert(Entity); |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 244 | SourceLocation BeginLoc = Entity->getSourceRange().getBegin(); |
| 245 | |
| 246 | // Check normal case, this entity begin location is after the previous one. |
| 247 | if (PreprocessedEntities.empty() || |
| 248 | !SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 249 | PreprocessedEntities.back()->getSourceRange().getBegin())) { |
| 250 | PreprocessedEntities.push_back(Entity); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // The entity's location is not after the previous one; this can happen rarely |
| 255 | // e.g. with "#include MACRO". |
| 256 | // Iterate the entities vector in reverse until we find the right place to |
| 257 | // insert the new entity. |
| 258 | for (std::vector<PreprocessedEntity *>::iterator |
| 259 | RI = PreprocessedEntities.end(), Begin = PreprocessedEntities.begin(); |
| 260 | RI != Begin; --RI) { |
| 261 | std::vector<PreprocessedEntity *>::iterator I = RI; |
| 262 | --I; |
| 263 | if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 264 | (*I)->getSourceRange().getBegin())) { |
| 265 | PreprocessedEntities.insert(RI, Entity); |
| 266 | return; |
| 267 | } |
| 268 | } |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 271 | void PreprocessingRecord::SetExternalSource( |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 272 | ExternalPreprocessingRecordSource &Source) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 273 | assert(!ExternalSource && |
| 274 | "Preprocessing record already has an external source"); |
| 275 | ExternalSource = &Source; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 278 | unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { |
| 279 | unsigned Result = LoadedPreprocessedEntities.size(); |
| 280 | LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size() |
| 281 | + NumEntities); |
| 282 | return Result; |
| 283 | } |
| 284 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 285 | void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, |
| 286 | PPEntityID PPID) { |
| 287 | MacroDefinitions[Macro] = PPID; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 290 | /// \brief Retrieve the preprocessed entity at the given ID. |
| 291 | PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){ |
| 292 | if (PPID < 0) { |
| 293 | assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() && |
| 294 | "Out-of bounds loaded preprocessed entity"); |
| 295 | return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID); |
| 296 | } |
| 297 | assert(unsigned(PPID) < PreprocessedEntities.size() && |
| 298 | "Out-of bounds local preprocessed entity"); |
| 299 | return PreprocessedEntities[PPID]; |
| 300 | } |
| 301 | |
| 302 | /// \brief Retrieve the loaded preprocessed entity at the given index. |
| 303 | PreprocessedEntity * |
| 304 | PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) { |
| 305 | assert(Index < LoadedPreprocessedEntities.size() && |
| 306 | "Out-of bounds loaded preprocessed entity"); |
| 307 | assert(ExternalSource && "No external source to load from"); |
| 308 | PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index]; |
| 309 | if (!Entity) { |
| 310 | Entity = ExternalSource->ReadPreprocessedEntity(Index); |
| 311 | if (!Entity) // Failed to load. |
| 312 | Entity = new (*this) |
| 313 | PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange()); |
| 314 | } |
| 315 | return Entity; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 318 | MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 319 | llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 320 | = MacroDefinitions.find(MI); |
| 321 | if (Pos == MacroDefinitions.end()) |
| 322 | return 0; |
| 323 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 324 | PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second); |
| 325 | if (Entity->isInvalid()) |
| 326 | return 0; |
| 327 | return cast<MacroDefinition>(Entity); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 330 | void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, |
| 331 | SourceRange Range) { |
Argyrios Kyrtzidis | 335c5a4 | 2012-02-25 02:41:16 +0000 | [diff] [blame^] | 332 | // We don't record nested macro expansions. |
| 333 | if (Id.getLocation().isMacroID()) |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 334 | return; |
| 335 | |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 336 | if (MI->isBuiltinMacro()) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 337 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 338 | new (*this) MacroExpansion(Id.getIdentifierInfo(),Range)); |
| 339 | else if (MacroDefinition *Def = findMacroDefinition(MI)) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 340 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 341 | new (*this) MacroExpansion(Def, Range)); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 344 | void PreprocessingRecord::MacroDefined(const Token &Id, |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 345 | const MacroInfo *MI) { |
| 346 | SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); |
| 347 | MacroDefinition *Def |
Argyrios Kyrtzidis | 0d48fb8 | 2011-09-20 22:14:48 +0000 | [diff] [blame] | 348 | = new (*this) MacroDefinition(Id.getIdentifierInfo(), R); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 349 | addPreprocessedEntity(Def); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 350 | MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1, |
| 351 | /*isLoaded=*/false); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 352 | } |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 353 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 354 | void PreprocessingRecord::MacroUndefined(const Token &Id, |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 355 | const MacroInfo *MI) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 356 | llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 357 | = MacroDefinitions.find(MI); |
| 358 | if (Pos != MacroDefinitions.end()) |
| 359 | MacroDefinitions.erase(Pos); |
| 360 | } |
| 361 | |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 362 | void PreprocessingRecord::InclusionDirective( |
| 363 | SourceLocation HashLoc, |
| 364 | const clang::Token &IncludeTok, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 365 | StringRef FileName, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 366 | bool IsAngled, |
| 367 | const FileEntry *File, |
| 368 | clang::SourceLocation EndLoc, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 369 | StringRef SearchPath, |
| 370 | StringRef RelativePath) { |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 371 | InclusionDirective::InclusionKind Kind = InclusionDirective::Include; |
| 372 | |
| 373 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 374 | case tok::pp_include: |
| 375 | Kind = InclusionDirective::Include; |
| 376 | break; |
| 377 | |
| 378 | case tok::pp_import: |
| 379 | Kind = InclusionDirective::Import; |
| 380 | break; |
| 381 | |
| 382 | case tok::pp_include_next: |
| 383 | Kind = InclusionDirective::IncludeNext; |
| 384 | break; |
| 385 | |
| 386 | case tok::pp___include_macros: |
| 387 | Kind = InclusionDirective::IncludeMacros; |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | llvm_unreachable("Unknown include directive kind"); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | clang::InclusionDirective *ID |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 395 | = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, |
| 396 | File, SourceRange(HashLoc, EndLoc)); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 397 | addPreprocessedEntity(ID); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 398 | } |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 399 | |
| 400 | size_t PreprocessingRecord::getTotalMemory() const { |
| 401 | return BumpAlloc.getTotalMemory() |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 402 | + llvm::capacity_in_bytes(MacroDefinitions) |
| 403 | + llvm::capacity_in_bytes(PreprocessedEntities) |
| 404 | + llvm::capacity_in_bytes(LoadedPreprocessedEntities); |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 405 | } |