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