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