Douglas Gregor | 4ae8f29 | 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 | e3c20a8 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Capacity.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 22 | ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { } |
| 23 | |
Douglas Gregor | 4ab829c | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 24 | |
| 25 | InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, |
| 26 | InclusionKind Kind, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 27 | StringRef FileName, |
Argyrios Kyrtzidis | 8dd927c | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 28 | bool InQuotes, bool ImportedModule, |
| 29 | const FileEntry *File, |
Douglas Gregor | 4ab829c | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 30 | SourceRange Range) |
| 31 | : PreprocessingDirective(InclusionDirectiveKind, Range), |
Argyrios Kyrtzidis | 8dd927c | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 32 | InQuotes(InQuotes), Kind(Kind), ImportedModule(ImportedModule), File(File) |
Douglas Gregor | 4ab829c | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 38 | this->FileName = StringRef(Memory, FileName.size()); |
Douglas Gregor | 4ab829c | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 41 | PreprocessingRecord::PreprocessingRecord(SourceManager &SM) |
Argyrios Kyrtzidis | c6c5452 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 42 | : SourceMgr(SM), |
Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 43 | ExternalSource(0) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 46 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
James Dennett | cd6b34b | 2012-06-22 05:37:13 +0000 | [diff] [blame] | 47 | /// that source range \p Range encompasses. |
Argyrios Kyrtzidis | 2dbaca7 | 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 | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 51 | return std::make_pair(iterator(), iterator()); |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 52 | |
Argyrios Kyrtzidis | f226ff9 | 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 | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 58 | std::pair<int, int> Res = getPreprocessedEntitiesInRangeSlow(Range); |
Argyrios Kyrtzidis | f226ff9 | 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 | cd6b34b | 2012-06-22 05:37:13 +0000 | [diff] [blame] | 87 | /// and not from files \#included in the range given at |
Argyrios Kyrtzidis | f226ff9 | 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 | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 93 | int Pos = PPEI.Position; |
| 94 | if (Pos < 0) { |
| 95 | assert(unsigned(-Pos-1) < LoadedPreprocessedEntities.size() && |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 96 | "Out-of bounds loaded preprocessed entity"); |
| 97 | assert(ExternalSource && "No external source to load from"); |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 98 | unsigned LoadedIndex = LoadedPreprocessedEntities.size()+Pos; |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 116 | assert(unsigned(Pos) < PreprocessedEntities.size() && |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 117 | "Out-of bounds local preprocessed entity"); |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 118 | return isPreprocessedEntityIfInFileID(PreprocessedEntities[Pos], |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 119 | FID, SourceMgr); |
| 120 | } |
| 121 | |
| 122 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
| 123 | /// that source range \arg R encompasses. |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 124 | std::pair<int, int> |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 125 | PreprocessingRecord::getPreprocessedEntitiesInRangeSlow(SourceRange Range) { |
| 126 | assert(Range.isValid()); |
| 127 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 128 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 129 | std::pair<unsigned, unsigned> |
| 130 | Local = findLocalPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 131 | |
Argyrios Kyrtzidis | 2dbaca7 | 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 | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 134 | return std::make_pair(Local.first, Local.second); |
| 135 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 136 | std::pair<unsigned, unsigned> |
| 137 | Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 138 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 139 | // Check if range spans local entities. |
| 140 | if (Loaded.first == Loaded.second) |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 141 | return std::make_pair(Local.first, Local.second); |
| 142 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 143 | unsigned TotalLoaded = LoadedPreprocessedEntities.size(); |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 144 | |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 145 | // Check if range spans loaded entities. |
| 146 | if (Local.first == Local.second) |
Argyrios Kyrtzidis | f226ff9 | 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 | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 150 | // Range spands loaded and local entities. |
Argyrios Kyrtzidis | f226ff9 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 151 | return std::make_pair(int(Loaded.first)-TotalLoaded, Local.second); |
Argyrios Kyrtzidis | 2dbaca7 | 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 | 196e71e | 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 | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 181 | SourceLocation LHS = getLoc(L); |
| 182 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 183 | } |
| 184 | |
Benjamin Kramer | 196e71e | 2011-09-21 16:58:20 +0000 | [diff] [blame] | 185 | bool operator()(SourceLocation LHS, PreprocessedEntity *R) const { |
Argyrios Kyrtzidis | 2dbaca7 | 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 | 0e322ff | 2011-09-19 22:02:08 +0000 | [diff] [blame] | 191 | SourceRange Range = PPE->getSourceRange(); |
| 192 | return (Range.*getRangeLoc)(); |
Argyrios Kyrtzidis | 2dbaca7 | 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 | 4cd0634 | 2011-09-22 21:17:02 +0000 | [diff] [blame] | 203 | size_t Count = PreprocessedEntities.size(); |
| 204 | size_t Half; |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 205 | std::vector<PreprocessedEntity *>::const_iterator |
Argyrios Kyrtzidis | 4cd0634 | 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 | 2dbaca7 | 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 | |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 242 | PreprocessingRecord::PPEntityID |
| 243 | PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { |
Argyrios Kyrtzidis | 2502efd | 2011-09-20 23:27:33 +0000 | [diff] [blame] | 244 | assert(Entity); |
Argyrios Kyrtzidis | 209dfbe | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 245 | SourceLocation BeginLoc = Entity->getSourceRange().getBegin(); |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 246 | |
| 247 | if (!isa<class InclusionDirective>(Entity)) { |
| 248 | assert((PreprocessedEntities.empty() || |
| 249 | !SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 250 | PreprocessedEntities.back()->getSourceRange().getBegin())) && |
| 251 | "a macro directive was encountered out-of-order"); |
| 252 | PreprocessedEntities.push_back(Entity); |
| 253 | return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false); |
| 254 | } |
| 255 | |
Argyrios Kyrtzidis | 209dfbe | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 256 | // Check normal case, this entity begin location is after the previous one. |
| 257 | if (PreprocessedEntities.empty() || |
| 258 | !SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 259 | PreprocessedEntities.back()->getSourceRange().getBegin())) { |
| 260 | PreprocessedEntities.push_back(Entity); |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 261 | return getPPEntityID(PreprocessedEntities.size()-1, /*isLoaded=*/false); |
Argyrios Kyrtzidis | 209dfbe | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 264 | // The entity's location is not after the previous one; this can happen with |
| 265 | // include directives that form the filename using macros, e.g: |
| 266 | // "#include MACRO(STUFF)". |
| 267 | |
| 268 | typedef std::vector<PreprocessedEntity *>::iterator pp_iter; |
| 269 | |
| 270 | // Usually there are few macro expansions when defining the filename, do a |
| 271 | // linear search for a few entities. |
| 272 | unsigned count = 0; |
| 273 | for (pp_iter RI = PreprocessedEntities.end(), |
| 274 | Begin = PreprocessedEntities.begin(); |
| 275 | RI != Begin && count < 4; --RI, ++count) { |
| 276 | pp_iter I = RI; |
Argyrios Kyrtzidis | 209dfbe | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 277 | --I; |
| 278 | if (!SourceMgr.isBeforeInTranslationUnit(BeginLoc, |
| 279 | (*I)->getSourceRange().getBegin())) { |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 280 | pp_iter insertI = PreprocessedEntities.insert(RI, Entity); |
| 281 | return getPPEntityID(insertI - PreprocessedEntities.begin(), |
| 282 | /*isLoaded=*/false); |
Argyrios Kyrtzidis | 209dfbe | 2011-10-12 17:36:33 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 285 | |
| 286 | // Linear search unsuccessful. Do a binary search. |
| 287 | pp_iter I = std::upper_bound(PreprocessedEntities.begin(), |
| 288 | PreprocessedEntities.end(), |
| 289 | BeginLoc, |
| 290 | PPEntityComp<&SourceRange::getBegin>(SourceMgr)); |
| 291 | pp_iter insertI = PreprocessedEntities.insert(I, Entity); |
| 292 | return getPPEntityID(insertI - PreprocessedEntities.begin(), |
| 293 | /*isLoaded=*/false); |
Douglas Gregor | 4ae8f29 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 296 | void PreprocessingRecord::SetExternalSource( |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 297 | ExternalPreprocessingRecordSource &Source) { |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 298 | assert(!ExternalSource && |
| 299 | "Preprocessing record already has an external source"); |
| 300 | ExternalSource = &Source; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Douglas Gregor | 4c30bb1 | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 303 | unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { |
| 304 | unsigned Result = LoadedPreprocessedEntities.size(); |
| 305 | LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size() |
| 306 | + NumEntities); |
| 307 | return Result; |
| 308 | } |
| 309 | |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 310 | void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, |
| 311 | PPEntityID PPID) { |
| 312 | MacroDefinitions[Macro] = PPID; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 315 | /// \brief Retrieve the preprocessed entity at the given ID. |
| 316 | PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){ |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 317 | if (PPID.ID < 0) { |
| 318 | unsigned Index = -PPID.ID - 1; |
| 319 | assert(Index < LoadedPreprocessedEntities.size() && |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 320 | "Out-of bounds loaded preprocessed entity"); |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 321 | return getLoadedPreprocessedEntity(Index); |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 322 | } |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 323 | |
| 324 | if (PPID.ID == 0) |
| 325 | return 0; |
| 326 | unsigned Index = PPID.ID - 1; |
| 327 | assert(Index < PreprocessedEntities.size() && |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 328 | "Out-of bounds local preprocessed entity"); |
Argyrios Kyrtzidis | f03b888 | 2012-10-05 00:22:28 +0000 | [diff] [blame] | 329 | return PreprocessedEntities[Index]; |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /// \brief Retrieve the loaded preprocessed entity at the given index. |
| 333 | PreprocessedEntity * |
| 334 | PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) { |
| 335 | assert(Index < LoadedPreprocessedEntities.size() && |
| 336 | "Out-of bounds loaded preprocessed entity"); |
| 337 | assert(ExternalSource && "No external source to load from"); |
| 338 | PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index]; |
| 339 | if (!Entity) { |
| 340 | Entity = ExternalSource->ReadPreprocessedEntity(Index); |
| 341 | if (!Entity) // Failed to load. |
| 342 | Entity = new (*this) |
| 343 | PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange()); |
| 344 | } |
| 345 | return Entity; |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | 1b058e8 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 348 | MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 349 | llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 350 | = MacroDefinitions.find(MI); |
| 351 | if (Pos == MacroDefinitions.end()) |
| 352 | return 0; |
| 353 | |
Argyrios Kyrtzidis | e24692b | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 354 | PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second); |
| 355 | if (Entity->isInvalid()) |
| 356 | return 0; |
| 357 | return cast<MacroDefinition>(Entity); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Argyrios Kyrtzidis | 5f5250b | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 360 | void PreprocessingRecord::addMacroExpansion(const Token &Id, |
| 361 | const MacroInfo *MI, |
| 362 | SourceRange Range) { |
Argyrios Kyrtzidis | e1d4330 | 2012-02-25 02:41:16 +0000 | [diff] [blame] | 363 | // We don't record nested macro expansions. |
| 364 | if (Id.getLocation().isMacroID()) |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 365 | return; |
| 366 | |
Argyrios Kyrtzidis | 8f7c540 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 367 | if (MI->isBuiltinMacro()) |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 368 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 8f7c540 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 369 | new (*this) MacroExpansion(Id.getIdentifierInfo(),Range)); |
| 370 | else if (MacroDefinition *Def = findMacroDefinition(MI)) |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 371 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 8f7c540 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 372 | new (*this) MacroExpansion(Def, Range)); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Argyrios Kyrtzidis | 5f5250b | 2012-12-08 02:21:17 +0000 | [diff] [blame] | 375 | void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, |
| 376 | const MacroInfo *MI) { |
| 377 | // This is not actually a macro expansion but record it as a macro reference. |
| 378 | if (MI) |
| 379 | addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation()); |
| 380 | } |
| 381 | |
| 382 | void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, |
| 383 | const MacroInfo *MI) { |
| 384 | // This is not actually a macro expansion but record it as a macro reference. |
| 385 | if (MI) |
| 386 | addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation()); |
| 387 | } |
| 388 | |
| 389 | void PreprocessingRecord::Defined(const Token &MacroNameTok, |
| 390 | const MacroInfo *MI) { |
| 391 | // This is not actually a macro expansion but record it as a macro reference. |
| 392 | if (MI) |
| 393 | addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation()); |
| 394 | } |
| 395 | |
| 396 | void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, |
| 397 | SourceRange Range) { |
| 398 | addMacroExpansion(Id, MI, Range); |
| 399 | } |
| 400 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 401 | void PreprocessingRecord::MacroDefined(const Token &Id, |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 402 | const MacroInfo *MI) { |
| 403 | SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); |
| 404 | MacroDefinition *Def |
Argyrios Kyrtzidis | de4e0a8 | 2011-09-20 22:14:48 +0000 | [diff] [blame] | 405 | = new (*this) MacroDefinition(Id.getIdentifierInfo(), R); |
Argyrios Kyrtzidis | db81d38 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 406 | MacroDefinitions[MI] = addPreprocessedEntity(Def); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 407 | } |
Douglas Gregor | 6a5a23f | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 408 | |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 409 | void PreprocessingRecord::MacroUndefined(const Token &Id, |
Douglas Gregor | 1b058e8 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 410 | const MacroInfo *MI) { |
Benjamin Kramer | 7e42392 | 2012-03-24 18:22:12 +0000 | [diff] [blame] | 411 | MacroDefinitions.erase(MI); |
Douglas Gregor | 1b058e8 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 414 | void PreprocessingRecord::InclusionDirective( |
| 415 | SourceLocation HashLoc, |
| 416 | const clang::Token &IncludeTok, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 417 | StringRef FileName, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 418 | bool IsAngled, |
Argyrios Kyrtzidis | da31359 | 2012-09-27 01:42:07 +0000 | [diff] [blame] | 419 | CharSourceRange FilenameRange, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 420 | const FileEntry *File, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 421 | StringRef SearchPath, |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 422 | StringRef RelativePath, |
| 423 | const Module *Imported) { |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 424 | InclusionDirective::InclusionKind Kind = InclusionDirective::Include; |
| 425 | |
| 426 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 427 | case tok::pp_include: |
| 428 | Kind = InclusionDirective::Include; |
| 429 | break; |
| 430 | |
| 431 | case tok::pp_import: |
| 432 | Kind = InclusionDirective::Import; |
| 433 | break; |
| 434 | |
| 435 | case tok::pp_include_next: |
| 436 | Kind = InclusionDirective::IncludeNext; |
| 437 | break; |
| 438 | |
| 439 | case tok::pp___include_macros: |
| 440 | Kind = InclusionDirective::IncludeMacros; |
| 441 | break; |
| 442 | |
| 443 | default: |
| 444 | llvm_unreachable("Unknown include directive kind"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 445 | } |
Argyrios Kyrtzidis | da31359 | 2012-09-27 01:42:07 +0000 | [diff] [blame] | 446 | |
| 447 | SourceLocation EndLoc; |
| 448 | if (!IsAngled) { |
| 449 | EndLoc = FilenameRange.getBegin(); |
| 450 | } else { |
| 451 | EndLoc = FilenameRange.getEnd(); |
| 452 | if (FilenameRange.isCharRange()) |
| 453 | EndLoc = EndLoc.getLocWithOffset(-1); // the InclusionDirective expects |
| 454 | // a token range. |
| 455 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 456 | clang::InclusionDirective *ID |
Argyrios Kyrtzidis | 8dd927c | 2012-10-02 16:10:46 +0000 | [diff] [blame] | 457 | = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, |
| 458 | (bool)Imported, |
Douglas Gregor | 4ab829c | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 459 | File, SourceRange(HashLoc, EndLoc)); |
Argyrios Kyrtzidis | 2dbaca7 | 2011-09-19 20:40:25 +0000 | [diff] [blame] | 460 | addPreprocessedEntity(ID); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 461 | } |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 462 | |
| 463 | size_t PreprocessingRecord::getTotalMemory() const { |
| 464 | return BumpAlloc.getTotalMemory() |
Ted Kremenek | e3c20a8 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 465 | + llvm::capacity_in_bytes(MacroDefinitions) |
| 466 | + llvm::capacity_in_bytes(PreprocessedEntities) |
| 467 | + llvm::capacity_in_bytes(LoadedPreprocessedEntities); |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 468 | } |