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" |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Capacity.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.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, |
Argyrios Kyrtzidis | f590e09 | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 28 | bool InQuotes, bool ImportedModule, |
| 29 | const FileEntry *File, |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 30 | SourceRange Range) |
| 31 | : PreprocessingDirective(InclusionDirectiveKind, Range), |
Argyrios Kyrtzidis | f590e09 | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 32 | InQuotes(InQuotes), Kind(Kind), ImportedModule(ImportedModule), File(File) |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 33 | { |
| 34 | char *Memory |
| 35 | = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>()); |
| 36 | memcpy(Memory, FileName.data(), FileName.size()); |
| 37 | Memory[FileName.size()] = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | this->FileName = StringRef(Memory, FileName.size()); |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 41 | PreprocessingRecord::PreprocessingRecord(SourceManager &SM) |
Argyrios Kyrtzidis | 647dcd8 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 42 | : SourceMgr(SM), |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 43 | ExternalSource(0) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 46 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
James Dennett | 303d8d4 | 2012-06-22 05:37:13 +0000 | [diff] [blame] | 47 | /// that source range \p Range encompasses. |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 48 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 49 | PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) { |
| 50 | if (Range.isInvalid()) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 51 | return std::make_pair(iterator(), iterator()); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 53 | if (CachedRangeQuery.Range == Range) { |
| 54 | return std::make_pair(iterator(this, CachedRangeQuery.Result.first), |
| 55 | iterator(this, CachedRangeQuery.Result.second)); |
| 56 | } |
| 57 | |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 58 | std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 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 |
James Dennett | 303d8d4 | 2012-06-22 05:37:13 +0000 | [diff] [blame] | 87 | /// and not from files \#included in the range given at |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 88 | /// \see getPreprocessedEntitiesInRange. |
| 89 | bool PreprocessingRecord::isEntityInFileID(iterator PPEI, FileID FID) { |
| 90 | if (FID.isInvalid()) |
| 91 | return false; |
| 92 | |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 93 | int Pos = PPEI.Position; |
| 94 | if (Pos < 0) { |
Argyrios Kyrtzidis | 962b221 | 2013-02-12 21:41:23 +0000 | [diff] [blame] | 95 | if (unsigned(-Pos-1) >= LoadedPreprocessedEntities.size()) { |
| 96 | assert(0 && "Out-of bounds loaded preprocessed entity"); |
| 97 | return false; |
| 98 | } |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 99 | assert(ExternalSource && "No external source to load from"); |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 100 | unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos; |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 101 | if (PreprocessedEntity *PPE = LoadedPreprocessedEntities[LoadedIndex]) |
| 102 | return isPreprocessedEntityIfInFileID(PPE, FID, SourceMgr); |
| 103 | |
| 104 | // See if the external source can see if the entity is in the file without |
| 105 | // deserializing it. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 106 | Optional<bool> IsInFile = |
| 107 | ExternalSource->isPreprocessedEntityInFileID(LoadedIndex, FID); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 108 | if (IsInFile.hasValue()) |
| 109 | return IsInFile.getValue(); |
| 110 | |
| 111 | // The external source did not provide a definite answer, go and deserialize |
| 112 | // the entity to check it. |
| 113 | return isPreprocessedEntityIfInFileID( |
| 114 | getLoadedPreprocessedEntity(LoadedIndex), |
| 115 | FID, SourceMgr); |
| 116 | } |
| 117 | |
Argyrios Kyrtzidis | 962b221 | 2013-02-12 21:41:23 +0000 | [diff] [blame] | 118 | if (unsigned(Pos) >= PreprocessedEntities.size()) { |
| 119 | assert(0 && "Out-of bounds local preprocessed entity"); |
| 120 | return false; |
| 121 | } |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 122 | return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos], |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 123 | FID, SourceMgr); |
| 124 | } |
| 125 | |
| 126 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
| 127 | /// that source range \arg R encompasses. |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 128 | std::pair<int, int> |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 129 | PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) { |
| 130 | assert(Range.isValid()); |
| 131 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 132 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 133 | std::pair<unsigned, unsigned> |
| 134 | Local = findLocalPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 135 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 136 | // Check if range spans local entities. |
| 137 | if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin())) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 138 | return std::make_pair(Local.first, Local.second); |
| 139 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 140 | std::pair<unsigned, unsigned> |
| 141 | Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 142 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 143 | // Check if range spans local entities. |
| 144 | if (Loaded.first == Loaded.second) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 145 | return std::make_pair(Local.first, Local.second); |
| 146 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 147 | unsigned TotalLoaded = LoadedPreprocessedEntities.size(); |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 148 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 149 | // Check if range spans loaded entities. |
| 150 | if (Local.first == Local.second) |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 151 | return std::make_pair(int(Loaded.first)-TotalLoaded, |
| 152 | int(Loaded.second)-TotalLoaded); |
| 153 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 154 | // Range spands loaded and local entities. |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 155 | return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | std::pair<unsigned, unsigned> |
| 159 | PreprocessingRecord::findLocalPreprocessedEntitiesInRange( |
| 160 | SourceRange Range) const { |
| 161 | if (Range.isInvalid()) |
| 162 | return std::make_pair(0,0); |
| 163 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 164 | |
| 165 | unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin()); |
| 166 | unsigned End = findEndLocalPreprocessedEntity(Range.getEnd()); |
| 167 | return std::make_pair(Begin, End); |
| 168 | } |
| 169 | |
| 170 | namespace { |
| 171 | |
| 172 | template <SourceLocation (SourceRange::*getRangeLoc)() const> |
| 173 | struct PPEntityComp { |
| 174 | const SourceManager &SM; |
| 175 | |
| 176 | explicit PPEntityComp(const SourceManager &SM) : SM(SM) { } |
| 177 | |
Benjamin Kramer | 2e9d9cf | 2011-09-21 16:58:20 +0000 | [diff] [blame] | 178 | bool operator()(PreprocessedEntity *L, PreprocessedEntity *R) const { |
| 179 | SourceLocation LHS = getLoc(L); |
| 180 | SourceLocation RHS = getLoc(R); |
| 181 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 182 | } |
| 183 | |
| 184 | bool operator()(PreprocessedEntity *L, SourceLocation RHS) const { |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 185 | SourceLocation LHS = getLoc(L); |
| 186 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 187 | } |
| 188 | |
Benjamin Kramer | 2e9d9cf | 2011-09-21 16:58:20 +0000 | [diff] [blame] | 189 | bool operator()(SourceLocation LHS, PreprocessedEntity *R) const { |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 190 | SourceLocation RHS = getLoc(R); |
| 191 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 192 | } |
| 193 | |
| 194 | SourceLocation getLoc(PreprocessedEntity *PPE) const { |
Argyrios Kyrtzidis | a35c444 | 2011-09-19 22:02:08 +0000 | [diff] [blame] | 195 | SourceRange Range = PPE->getSourceRange(); |
| 196 | return (Range.*getRangeLoc)(); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity( |
| 203 | SourceLocation Loc) const { |
| 204 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 205 | return 0; |
| 206 | |
Argyrios Kyrtzidis | e523e38 | 2011-09-22 21:17:02 +0000 | [diff] [blame] | 207 | size_t Count = PreprocessedEntities.size(); |
| 208 | size_t Half; |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 209 | std::vector<PreprocessedEntity *>::const_iterator |
Argyrios Kyrtzidis | e523e38 | 2011-09-22 21:17:02 +0000 | [diff] [blame] | 210 | First = PreprocessedEntities.begin(); |
| 211 | std::vector<PreprocessedEntity *>::const_iterator I; |
| 212 | |
| 213 | // Do a binary search manually instead of using std::lower_bound because |
| 214 | // The end locations of entities may be unordered (when a macro expansion |
| 215 | // is inside another macro argument), but for this case it is not important |
| 216 | // whether we get the first macro expansion or its containing macro. |
| 217 | while (Count > 0) { |
| 218 | Half = Count/2; |
| 219 | I = First; |
| 220 | std::advance(I, Half); |
| 221 | if (SourceMgr.isBeforeInTranslationUnit((*I)->getSourceRange().getEnd(), |
| 222 | Loc)){ |
| 223 | First = I; |
| 224 | ++First; |
| 225 | Count = Count - Half - 1; |
| 226 | } else |
| 227 | Count = Half; |
| 228 | } |
| 229 | |
| 230 | return First - PreprocessedEntities.begin(); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | unsigned PreprocessingRecord::findEndLocalPreprocessedEntity( |
| 234 | SourceLocation Loc) const { |
| 235 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 236 | return 0; |
| 237 | |
| 238 | std::vector<PreprocessedEntity *>::const_iterator |
| 239 | I = std::upper_bound(PreprocessedEntities.begin(), |
| 240 | PreprocessedEntities.end(), |
| 241 | Loc, |
| 242 | PPEntityComp<&SourceRange::getBegin>(SourceMgr)); |
| 243 | return I - PreprocessedEntities.begin(); |
| 244 | } |
| 245 | |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 246 | PreprocessingRecord::PPEntityID |
| 247 | PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { |
Argyrios Kyrtzidis | 45e8cf5 | 2011-09-20 23:27:33 +0000 | [diff] [blame] | 248 | assert(Entity); |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 249 | SourceLocation BeginLoc = Entity->getSourceRange().getBegin(); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 250 | |
Argyrios Kyrtzidis | eb994f4 | 2013-01-09 23:22:20 +0000 | [diff] [blame] | 251 | if (isa<MacroDefinition>(Entity)) { |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 252 | assert((PreprocessedEntities.empty() || |
| 253 | !SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 254 | PreprocessedEntities.back()->getSourceRange().getBegin())) && |
Argyrios Kyrtzidis | eb994f4 | 2013-01-09 23:22:20 +0000 | [diff] [blame] | 255 | "a macro definition was encountered out-of-order"); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 256 | PreprocessedEntities.push_back(Entity); |
| 257 | return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false); |
| 258 | } |
| 259 | |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 260 | // Check normal case, this entity begin location is after the previous one. |
| 261 | if (PreprocessedEntities.empty() || |
| 262 | !SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 263 | PreprocessedEntities.back()->getSourceRange().getBegin())) { |
| 264 | PreprocessedEntities.push_back(Entity); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 265 | return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false); |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 268 | // The entity's location is not after the previous one; this can happen with |
| 269 | // include directives that form the filename using macros, e.g: |
Argyrios Kyrtzidis | eb994f4 | 2013-01-09 23:22:20 +0000 | [diff] [blame] | 270 | // "#include MACRO(STUFF)" |
| 271 | // or with macro expansions inside macro arguments where the arguments are |
| 272 | // not expanded in the same order as listed, e.g: |
| 273 | // \code |
| 274 | // #define M1 1 |
| 275 | // #define M2 2 |
| 276 | // #define FM(x,y) y x |
| 277 | // FM(M1, M2) |
| 278 | // \endcode |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 279 | |
| 280 | typedef std::vector<PreprocessedEntity *>::iterator pp_iter; |
| 281 | |
| 282 | // Usually there are few macro expansions when defining the filename, do a |
| 283 | // linear search for a few entities. |
| 284 | unsigned count = 0; |
| 285 | for (pp_iter RI = PreprocessedEntities.end(), |
| 286 | Begin = PreprocessedEntities.begin(); |
| 287 | RI != Begin && count < 4; --RI, ++count) { |
| 288 | pp_iter I = RI; |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 289 | --I; |
| 290 | if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 291 | (*I)->getSourceRange().getBegin())) { |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 292 | pp_iter insertI = PreprocessedEntities.insert(RI, Entity); |
| 293 | return getPPEntityID(insertI - PreprocessedEntities.begin(), |
| 294 | /*isLoaded=*/false); |
Argyrios Kyrtzidis | f37d0a6 | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 297 | |
| 298 | // Linear search unsuccessful. Do a binary search. |
| 299 | pp_iter I = std::upper_bound(PreprocessedEntities.begin(), |
| 300 | PreprocessedEntities.end(), |
| 301 | BeginLoc, |
| 302 | PPEntityComp<&SourceRange::getBegin>(SourceMgr)); |
| 303 | pp_iter insertI = PreprocessedEntities.insert(I, Entity); |
| 304 | return getPPEntityID(insertI - PreprocessedEntities.begin(), |
| 305 | /*isLoaded=*/false); |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 308 | void PreprocessingRecord::SetExternalSource( |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 309 | ExternalPreprocessingRecordSource &Source) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 310 | assert(!ExternalSource && |
| 311 | "Preprocessing record already has an external source"); |
| 312 | ExternalSource = &Source; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 315 | unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { |
| 316 | unsigned Result = LoadedPreprocessedEntities.size(); |
| 317 | LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size() |
| 318 | + NumEntities); |
| 319 | return Result; |
| 320 | } |
| 321 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 322 | void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, |
Argyrios Kyrtzidis | 832de9f | 2013-02-22 18:35:59 +0000 | [diff] [blame] | 323 | MacroDefinition *Def) { |
| 324 | MacroDefinitions[Macro] = Def; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 327 | /// \brief Retrieve the preprocessed entity at the given ID. |
| 328 | PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){ |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 329 | if (PPID.ID < 0) { |
| 330 | unsigned Index = -PPID.ID - 1; |
| 331 | assert(Index < LoadedPreprocessedEntities.size() && |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 332 | "Out-of bounds loaded preprocessed entity"); |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 333 | return getLoadedPreprocessedEntity(Index); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 334 | } |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 335 | |
| 336 | if (PPID.ID == 0) |
| 337 | return 0; |
| 338 | unsigned Index = PPID.ID - 1; |
| 339 | assert(Index < PreprocessedEntities.size() && |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 340 | "Out-of bounds local preprocessed entity"); |
Argyrios Kyrtzidis | abc721a | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 341 | return PreprocessedEntities[Index]; |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /// \brief Retrieve the loaded preprocessed entity at the given index. |
| 345 | PreprocessedEntity * |
| 346 | PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) { |
| 347 | assert(Index < LoadedPreprocessedEntities.size() && |
| 348 | "Out-of bounds loaded preprocessed entity"); |
| 349 | assert(ExternalSource && "No external source to load from"); |
| 350 | PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index]; |
| 351 | if (!Entity) { |
| 352 | Entity = ExternalSource->ReadPreprocessedEntity(Index); |
| 353 | if (!Entity) // Failed to load. |
| 354 | Entity = new (*this) |
| 355 | PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange()); |
| 356 | } |
| 357 | return Entity; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 360 | MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { |
Argyrios Kyrtzidis | 832de9f | 2013-02-22 18:35:59 +0000 | [diff] [blame] | 361 | llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 362 | = MacroDefinitions.find(MI); |
| 363 | if (Pos == MacroDefinitions.end()) |
| 364 | return 0; |
Argyrios Kyrtzidis | 832de9f | 2013-02-22 18:35:59 +0000 | [diff] [blame] | 365 | |
| 366 | return Pos->second; |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 369 | void PreprocessingRecord::addMacroExpansion(const Token &Id, |
| 370 | const MacroInfo *MI, |
| 371 | SourceRange Range) { |
Argyrios Kyrtzidis | 335c5a4 | 2012-02-25 02:41:16 +0000 | [diff] [blame] | 372 | // We don't record nested macro expansions. |
| 373 | if (Id.getLocation().isMacroID()) |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 374 | return; |
| 375 | |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 376 | if (MI->isBuiltinMacro()) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 377 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 378 | new (*this) MacroExpansion(Id.getIdentifierInfo(),Range)); |
| 379 | else if (MacroDefinition *Def = findMacroDefinition(MI)) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 380 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 381 | new (*this) MacroExpansion(Def, Range)); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 384 | void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 385 | const MacroDirective *MD) { |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 386 | // This is not actually a macro expansion but record it as a macro reference. |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 387 | if (MD) |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 388 | addMacroExpansion(MacroNameTok, MD->getMacroInfo(), |
| 389 | MacroNameTok.getLocation()); |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 393 | const MacroDirective *MD) { |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 394 | // This is not actually a macro expansion but record it as a macro reference. |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 395 | if (MD) |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 396 | addMacroExpansion(MacroNameTok, MD->getMacroInfo(), |
| 397 | MacroNameTok.getLocation()); |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void PreprocessingRecord::Defined(const Token &MacroNameTok, |
John Thompson | cda95fe | 2013-07-19 18:50:04 +0000 | [diff] [blame] | 401 | const MacroDirective *MD, |
| 402 | SourceRange Range) { |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 403 | // This is not actually a macro expansion but record it as a macro reference. |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 404 | if (MD) |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 405 | addMacroExpansion(MacroNameTok, MD->getMacroInfo(), |
| 406 | MacroNameTok.getLocation()); |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame^] | 409 | void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) { |
| 410 | SkippedRanges.push_back(Range); |
| 411 | } |
| 412 | |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 413 | void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD, |
Argyrios Kyrtzidis | 37e48ff | 2013-05-03 22:31:32 +0000 | [diff] [blame] | 414 | SourceRange Range, |
| 415 | const MacroArgs *Args) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 416 | addMacroExpansion(Id, MD->getMacroInfo(), Range); |
Argyrios Kyrtzidis | f77b0f8 | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 419 | void PreprocessingRecord::MacroDefined(const Token &Id, |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 420 | const MacroDirective *MD) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 421 | const MacroInfo *MI = MD->getMacroInfo(); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 422 | SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); |
| 423 | MacroDefinition *Def |
Argyrios Kyrtzidis | 0d48fb8 | 2011-09-20 22:14:48 +0000 | [diff] [blame] | 424 | = new (*this) MacroDefinition(Id.getIdentifierInfo(), R); |
Argyrios Kyrtzidis | 832de9f | 2013-02-22 18:35:59 +0000 | [diff] [blame] | 425 | addPreprocessedEntity(Def); |
| 426 | MacroDefinitions[MI] = Def; |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 427 | } |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 428 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 429 | void PreprocessingRecord::MacroUndefined(const Token &Id, |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 430 | const MacroDirective *MD) { |
Argyrios Kyrtzidis | 99b0a6a | 2013-01-16 16:52:44 +0000 | [diff] [blame] | 431 | // Note: MI may be null (when #undef'ining an undefined macro). |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 432 | if (MD) |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 433 | MacroDefinitions.erase(MD->getMacroInfo()); |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 436 | void PreprocessingRecord::InclusionDirective( |
| 437 | SourceLocation HashLoc, |
| 438 | const clang::Token &IncludeTok, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 439 | StringRef FileName, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 440 | bool IsAngled, |
Argyrios Kyrtzidis | 4fcd288 | 2012-09-27 01:42:07 +0000 | [diff] [blame] | 441 | CharSourceRange FilenameRange, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 442 | const FileEntry *File, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 443 | StringRef SearchPath, |
Argyrios Kyrtzidis | 19d78b7 | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 444 | StringRef RelativePath, |
| 445 | const Module *Imported) { |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 446 | InclusionDirective::InclusionKind Kind = InclusionDirective::Include; |
| 447 | |
| 448 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 449 | case tok::pp_include: |
| 450 | Kind = InclusionDirective::Include; |
| 451 | break; |
| 452 | |
| 453 | case tok::pp_import: |
| 454 | Kind = InclusionDirective::Import; |
| 455 | break; |
| 456 | |
| 457 | case tok::pp_include_next: |
| 458 | Kind = InclusionDirective::IncludeNext; |
| 459 | break; |
| 460 | |
| 461 | case tok::pp___include_macros: |
| 462 | Kind = InclusionDirective::IncludeMacros; |
| 463 | break; |
| 464 | |
| 465 | default: |
| 466 | llvm_unreachable("Unknown include directive kind"); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 467 | } |
Argyrios Kyrtzidis | 4fcd288 | 2012-09-27 01:42:07 +0000 | [diff] [blame] | 468 | |
| 469 | SourceLocation EndLoc; |
| 470 | if (!IsAngled) { |
| 471 | EndLoc = FilenameRange.getBegin(); |
| 472 | } else { |
| 473 | EndLoc = FilenameRange.getEnd(); |
| 474 | if (FilenameRange.isCharRange()) |
| 475 | EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects |
| 476 | // a token range. |
| 477 | } |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 478 | clang::InclusionDirective *ID |
Argyrios Kyrtzidis | f590e09 | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 479 | = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, |
| 480 | (bool)Imported, |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 481 | File, SourceRange(HashLoc, EndLoc)); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 482 | addPreprocessedEntity(ID); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 483 | } |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 484 | |
| 485 | size_t PreprocessingRecord::getTotalMemory() const { |
| 486 | return BumpAlloc.getTotalMemory() |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 487 | + llvm::capacity_in_bytes(MacroDefinitions) |
| 488 | + llvm::capacity_in_bytes(PreprocessedEntities) |
| 489 | + llvm::capacity_in_bytes(LoadedPreprocessedEntities); |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 490 | } |