Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 1 | //===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PreprocessingRecord class, which maintains a record |
| 11 | // of what occurred during preprocessing, and its helpers. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/Lex/PreprocessingRecord.h" |
| 15 | #include "clang/Lex/MacroInfo.h" |
| 16 | #include "clang/Lex/Token.h" |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Capacity.h" |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 22 | ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { } |
| 23 | |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 24 | |
| 25 | InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, |
| 26 | InclusionKind Kind, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 27 | StringRef FileName, |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 28 | bool InQuotes, const FileEntry *File, |
| 29 | SourceRange Range) |
| 30 | : PreprocessingDirective(InclusionDirectiveKind, Range), |
| 31 | InQuotes(InQuotes), Kind(Kind), File(File) |
| 32 | { |
| 33 | char *Memory |
| 34 | = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>()); |
| 35 | memcpy(Memory, FileName.data(), FileName.size()); |
| 36 | Memory[FileName.size()] = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | this->FileName = StringRef(Memory, FileName.size()); |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 40 | PreprocessingRecord::PreprocessingRecord(SourceManager &SM, |
| 41 | bool IncludeNestedMacroExpansions) |
| 42 | : SourceMgr(SM), IncludeNestedMacroExpansions(IncludeNestedMacroExpansions), |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 43 | ExternalSource(0) |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | PreprocessingRecord::iterator |
| 48 | PreprocessingRecord::begin(bool OnlyLocalEntities) { |
| 49 | if (OnlyLocalEntities) |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 50 | return iterator(this, 0); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 52 | return iterator(this, -(int)LoadedPreprocessedEntities.size()); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) { |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 56 | return iterator(this, PreprocessedEntities.size()); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 59 | /// \brief Returns a pair of [Begin, End) iterators of preprocessed entities |
| 60 | /// that source range \arg R encompasses. |
| 61 | std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator> |
| 62 | PreprocessingRecord::getPreprocessedEntitiesInRange(SourceRange Range) { |
| 63 | if (Range.isInvalid()) |
| 64 | return std::make_pair(iterator(this, 0), iterator(this, 0)); |
| 65 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 66 | |
| 67 | std::pair<unsigned, unsigned> |
| 68 | Local = findLocalPreprocessedEntitiesInRange(Range); |
| 69 | |
| 70 | // Check if range spans local entities. |
| 71 | if (!ExternalSource || SourceMgr.isLocalSourceLocation(Range.getBegin())) |
| 72 | return std::make_pair(iterator(this, Local.first), |
| 73 | iterator(this, Local.second)); |
| 74 | |
| 75 | std::pair<unsigned, unsigned> |
| 76 | Loaded = ExternalSource->findPreprocessedEntitiesInRange(Range); |
| 77 | |
| 78 | // Check if range spans local entities. |
| 79 | if (Loaded.first == Loaded.second) |
| 80 | return std::make_pair(iterator(this, Local.first), |
| 81 | iterator(this, Local.second)); |
| 82 | |
| 83 | unsigned TotalLoaded = LoadedPreprocessedEntities.size(); |
| 84 | |
| 85 | // Check if range spans loaded entities. |
| 86 | if (Local.first == Local.second) |
| 87 | return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded), |
| 88 | iterator(this, int(Loaded.second)-TotalLoaded)); |
| 89 | |
| 90 | // Range spands loaded and local entities. |
| 91 | return std::make_pair(iterator(this, int(Loaded.first)-TotalLoaded), |
| 92 | iterator(this, Local.second)); |
| 93 | } |
| 94 | |
| 95 | std::pair<unsigned, unsigned> |
| 96 | PreprocessingRecord::findLocalPreprocessedEntitiesInRange( |
| 97 | SourceRange Range) const { |
| 98 | if (Range.isInvalid()) |
| 99 | return std::make_pair(0,0); |
| 100 | assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); |
| 101 | |
| 102 | unsigned Begin = findBeginLocalPreprocessedEntity(Range.getBegin()); |
| 103 | unsigned End = findEndLocalPreprocessedEntity(Range.getEnd()); |
| 104 | return std::make_pair(Begin, End); |
| 105 | } |
| 106 | |
| 107 | namespace { |
| 108 | |
| 109 | template <SourceLocation (SourceRange::*getRangeLoc)() const> |
| 110 | struct PPEntityComp { |
| 111 | const SourceManager &SM; |
| 112 | |
| 113 | explicit PPEntityComp(const SourceManager &SM) : SM(SM) { } |
| 114 | |
| 115 | bool operator()(PreprocessedEntity *L, SourceLocation RHS) { |
| 116 | SourceLocation LHS = getLoc(L); |
| 117 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 118 | } |
| 119 | |
| 120 | bool operator()(SourceLocation LHS, PreprocessedEntity *R) { |
| 121 | SourceLocation RHS = getLoc(R); |
| 122 | return SM.isBeforeInTranslationUnit(LHS, RHS); |
| 123 | } |
| 124 | |
| 125 | SourceLocation getLoc(PreprocessedEntity *PPE) const { |
| 126 | return (PPE->getSourceRange().*getRangeLoc)(); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | unsigned PreprocessingRecord::findBeginLocalPreprocessedEntity( |
| 133 | SourceLocation Loc) const { |
| 134 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 135 | return 0; |
| 136 | |
| 137 | std::vector<PreprocessedEntity *>::const_iterator |
| 138 | I = std::lower_bound(PreprocessedEntities.begin(), |
| 139 | PreprocessedEntities.end(), |
| 140 | Loc, |
| 141 | PPEntityComp<&SourceRange::getEnd>(SourceMgr)); |
| 142 | return I - PreprocessedEntities.begin(); |
| 143 | } |
| 144 | |
| 145 | unsigned PreprocessingRecord::findEndLocalPreprocessedEntity( |
| 146 | SourceLocation Loc) const { |
| 147 | if (SourceMgr.isLoadedSourceLocation(Loc)) |
| 148 | return 0; |
| 149 | |
| 150 | std::vector<PreprocessedEntity *>::const_iterator |
| 151 | I = std::upper_bound(PreprocessedEntities.begin(), |
| 152 | PreprocessedEntities.end(), |
| 153 | Loc, |
| 154 | PPEntityComp<&SourceRange::getBegin>(SourceMgr)); |
| 155 | return I - PreprocessedEntities.begin(); |
| 156 | } |
| 157 | |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 158 | void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 159 | SourceLocation Loc = Entity->getSourceRange().getBegin(); |
| 160 | assert((PreprocessedEntities.empty() || |
| 161 | !SourceMgr.isBeforeInTranslationUnit(Loc, |
| 162 | PreprocessedEntities.back()->getSourceRange().getEnd())) && |
| 163 | "Adding a preprocessed entity that is before the previous one in TU"); |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 164 | PreprocessedEntities.push_back(Entity); |
| 165 | } |
| 166 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 167 | void PreprocessingRecord::SetExternalSource( |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 168 | ExternalPreprocessingRecordSource &Source) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 169 | assert(!ExternalSource && |
| 170 | "Preprocessing record already has an external source"); |
| 171 | ExternalSource = &Source; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 174 | unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { |
| 175 | unsigned Result = LoadedPreprocessedEntities.size(); |
| 176 | LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size() |
| 177 | + NumEntities); |
| 178 | return Result; |
| 179 | } |
| 180 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 181 | void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, |
| 182 | PPEntityID PPID) { |
| 183 | MacroDefinitions[Macro] = PPID; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 186 | /// \brief Retrieve the preprocessed entity at the given ID. |
| 187 | PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){ |
| 188 | if (PPID < 0) { |
| 189 | assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() && |
| 190 | "Out-of bounds loaded preprocessed entity"); |
| 191 | return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID); |
| 192 | } |
| 193 | assert(unsigned(PPID) < PreprocessedEntities.size() && |
| 194 | "Out-of bounds local preprocessed entity"); |
| 195 | return PreprocessedEntities[PPID]; |
| 196 | } |
| 197 | |
| 198 | /// \brief Retrieve the loaded preprocessed entity at the given index. |
| 199 | PreprocessedEntity * |
| 200 | PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) { |
| 201 | assert(Index < LoadedPreprocessedEntities.size() && |
| 202 | "Out-of bounds loaded preprocessed entity"); |
| 203 | assert(ExternalSource && "No external source to load from"); |
| 204 | PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index]; |
| 205 | if (!Entity) { |
| 206 | Entity = ExternalSource->ReadPreprocessedEntity(Index); |
| 207 | if (!Entity) // Failed to load. |
| 208 | Entity = new (*this) |
| 209 | PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange()); |
| 210 | } |
| 211 | return Entity; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 214 | MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 215 | llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 216 | = MacroDefinitions.find(MI); |
| 217 | if (Pos == MacroDefinitions.end()) |
| 218 | return 0; |
| 219 | |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 220 | PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second); |
| 221 | if (Entity->isInvalid()) |
| 222 | return 0; |
| 223 | return cast<MacroDefinition>(Entity); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 226 | void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, |
| 227 | SourceRange Range) { |
Chandler Carruth | a88a2218 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 228 | if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID()) |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 229 | return; |
| 230 | |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 231 | if (MI->isBuiltinMacro()) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 232 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 233 | new (*this) MacroExpansion(Id.getIdentifierInfo(),Range)); |
| 234 | else if (MacroDefinition *Def = findMacroDefinition(MI)) |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 235 | addPreprocessedEntity( |
Argyrios Kyrtzidis | 80f78b9 | 2011-09-08 17:18:41 +0000 | [diff] [blame] | 236 | new (*this) MacroExpansion(Def, Range)); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 239 | void PreprocessingRecord::MacroDefined(const Token &Id, |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 240 | const MacroInfo *MI) { |
| 241 | SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); |
| 242 | MacroDefinition *Def |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 243 | = new (*this) MacroDefinition(Id.getIdentifierInfo(), |
| 244 | MI->getDefinitionLoc(), |
| 245 | R); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 246 | addPreprocessedEntity(Def); |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 247 | MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1, |
| 248 | /*isLoaded=*/false); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 249 | } |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 250 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 251 | void PreprocessingRecord::MacroUndefined(const Token &Id, |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 252 | const MacroInfo *MI) { |
Argyrios Kyrtzidis | 03c40c5 | 2011-09-15 18:02:56 +0000 | [diff] [blame] | 253 | llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 254 | = MacroDefinitions.find(MI); |
| 255 | if (Pos != MacroDefinitions.end()) |
| 256 | MacroDefinitions.erase(Pos); |
| 257 | } |
| 258 | |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 259 | void PreprocessingRecord::InclusionDirective( |
| 260 | SourceLocation HashLoc, |
| 261 | const clang::Token &IncludeTok, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 262 | StringRef FileName, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 263 | bool IsAngled, |
| 264 | const FileEntry *File, |
| 265 | clang::SourceLocation EndLoc, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 266 | StringRef SearchPath, |
| 267 | StringRef RelativePath) { |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 268 | InclusionDirective::InclusionKind Kind = InclusionDirective::Include; |
| 269 | |
| 270 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 271 | case tok::pp_include: |
| 272 | Kind = InclusionDirective::Include; |
| 273 | break; |
| 274 | |
| 275 | case tok::pp_import: |
| 276 | Kind = InclusionDirective::Import; |
| 277 | break; |
| 278 | |
| 279 | case tok::pp_include_next: |
| 280 | Kind = InclusionDirective::IncludeNext; |
| 281 | break; |
| 282 | |
| 283 | case tok::pp___include_macros: |
| 284 | Kind = InclusionDirective::IncludeMacros; |
| 285 | break; |
| 286 | |
| 287 | default: |
| 288 | llvm_unreachable("Unknown include directive kind"); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | clang::InclusionDirective *ID |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 293 | = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, |
| 294 | File, SourceRange(HashLoc, EndLoc)); |
Argyrios Kyrtzidis | 64f6381 | 2011-09-19 20:40:25 +0000 | [diff] [blame^] | 295 | addPreprocessedEntity(ID); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 296 | } |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 297 | |
| 298 | size_t PreprocessingRecord::getTotalMemory() const { |
| 299 | return BumpAlloc.getTotalMemory() |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 300 | + llvm::capacity_in_bytes(MacroDefinitions) |
| 301 | + llvm::capacity_in_bytes(PreprocessedEntities) |
| 302 | + llvm::capacity_in_bytes(LoadedPreprocessedEntities); |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 303 | } |