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 "clang/Basic/IdentifierTable.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Capacity.h" |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 23 | ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { } |
| 24 | |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 25 | |
| 26 | InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec, |
| 27 | InclusionKind Kind, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 28 | StringRef FileName, |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 29 | bool InQuotes, const FileEntry *File, |
| 30 | SourceRange Range) |
| 31 | : PreprocessingDirective(InclusionDirectiveKind, Range), |
| 32 | InQuotes(InQuotes), Kind(Kind), File(File) |
| 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 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 41 | void PreprocessingRecord::MaybeLoadPreallocatedEntities() const { |
| 42 | if (!ExternalSource || LoadedPreallocatedEntities) |
| 43 | return; |
| 44 | |
| 45 | LoadedPreallocatedEntities = true; |
| 46 | ExternalSource->ReadPreprocessedEntities(); |
| 47 | } |
| 48 | |
Chandler Carruth | a88a2218 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 49 | PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroExpansions) |
| 50 | : IncludeNestedMacroExpansions(IncludeNestedMacroExpansions), |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 51 | ExternalSource(0), LoadedPreallocatedEntities(false) |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | PreprocessingRecord::iterator |
| 56 | PreprocessingRecord::begin(bool OnlyLocalEntities) { |
| 57 | if (OnlyLocalEntities) |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 58 | return iterator(this, 0); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 59 | |
| 60 | MaybeLoadPreallocatedEntities(); |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 61 | return iterator(this, -(int)LoadedPreprocessedEntities.size()); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) { |
| 65 | if (!OnlyLocalEntities) |
| 66 | MaybeLoadPreallocatedEntities(); |
| 67 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 68 | return iterator(this, PreprocessedEntities.size()); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Douglas Gregor | 065f8d1 | 2010-03-18 17:52:52 +0000 | [diff] [blame] | 71 | void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) { |
| 72 | PreprocessedEntities.push_back(Entity); |
| 73 | } |
| 74 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 75 | void PreprocessingRecord::SetExternalSource( |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 76 | ExternalPreprocessingRecordSource &Source) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 77 | assert(!ExternalSource && |
| 78 | "Preprocessing record already has an external source"); |
| 79 | ExternalSource = &Source; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Douglas Gregor | 4a9c39a | 2011-07-21 00:47:40 +0000 | [diff] [blame] | 82 | unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) { |
| 83 | unsigned Result = LoadedPreprocessedEntities.size(); |
| 84 | LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size() |
| 85 | + NumEntities); |
| 86 | return Result; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | PreprocessingRecord::setLoadedPreallocatedEntity(unsigned Index, |
| 91 | PreprocessedEntity *Entity) { |
| 92 | assert(Index < LoadedPreprocessedEntities.size() && |
| 93 | "Out-of-bounds preallocated entity"); |
| 94 | LoadedPreprocessedEntities[Index] = Entity; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, |
| 98 | MacroDefinition *MD) { |
| 99 | MacroDefinitions[Macro] = MD; |
| 100 | } |
| 101 | |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 102 | MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) { |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 103 | llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos |
| 104 | = MacroDefinitions.find(MI); |
| 105 | if (Pos == MacroDefinitions.end()) |
| 106 | return 0; |
| 107 | |
| 108 | return Pos->second; |
| 109 | } |
| 110 | |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 111 | void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI, |
| 112 | SourceRange Range) { |
Chandler Carruth | a88a2218 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 113 | if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID()) |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 114 | return; |
| 115 | |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 116 | if (MacroDefinition *Def = findMacroDefinition(MI)) |
| 117 | PreprocessedEntities.push_back( |
Chandler Carruth | a88a2218 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 118 | new (*this) MacroExpansion(Id.getIdentifierInfo(), |
Argyrios Kyrtzidis | 85a14bb | 2011-08-18 01:05:45 +0000 | [diff] [blame] | 119 | Range, Def)); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 122 | void PreprocessingRecord::MacroDefined(const Token &Id, |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 123 | const MacroInfo *MI) { |
| 124 | SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); |
| 125 | MacroDefinition *Def |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 126 | = new (*this) MacroDefinition(Id.getIdentifierInfo(), |
| 127 | MI->getDefinitionLoc(), |
| 128 | R); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 129 | MacroDefinitions[MI] = Def; |
| 130 | PreprocessedEntities.push_back(Def); |
| 131 | } |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 132 | |
Craig Silverstein | 1a9ca21 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 133 | void PreprocessingRecord::MacroUndefined(const Token &Id, |
Douglas Gregor | 8aaca67 | 2010-03-19 21:58:23 +0000 | [diff] [blame] | 134 | const MacroInfo *MI) { |
| 135 | llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos |
| 136 | = MacroDefinitions.find(MI); |
| 137 | if (Pos != MacroDefinitions.end()) |
| 138 | MacroDefinitions.erase(Pos); |
| 139 | } |
| 140 | |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 141 | void PreprocessingRecord::InclusionDirective( |
| 142 | SourceLocation HashLoc, |
| 143 | const clang::Token &IncludeTok, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 144 | StringRef FileName, |
Chandler Carruth | 3cc331a | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 145 | bool IsAngled, |
| 146 | const FileEntry *File, |
| 147 | clang::SourceLocation EndLoc, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 148 | StringRef SearchPath, |
| 149 | StringRef RelativePath) { |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 150 | InclusionDirective::InclusionKind Kind = InclusionDirective::Include; |
| 151 | |
| 152 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 153 | case tok::pp_include: |
| 154 | Kind = InclusionDirective::Include; |
| 155 | break; |
| 156 | |
| 157 | case tok::pp_import: |
| 158 | Kind = InclusionDirective::Import; |
| 159 | break; |
| 160 | |
| 161 | case tok::pp_include_next: |
| 162 | Kind = InclusionDirective::IncludeNext; |
| 163 | break; |
| 164 | |
| 165 | case tok::pp___include_macros: |
| 166 | Kind = InclusionDirective::IncludeMacros; |
| 167 | break; |
| 168 | |
| 169 | default: |
| 170 | llvm_unreachable("Unknown include directive kind"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | clang::InclusionDirective *ID |
Douglas Gregor | f09b6c9 | 2010-11-01 15:03:47 +0000 | [diff] [blame] | 175 | = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, |
| 176 | File, SourceRange(HashLoc, EndLoc)); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 177 | PreprocessedEntities.push_back(ID); |
| 178 | } |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 179 | |
| 180 | size_t PreprocessingRecord::getTotalMemory() const { |
| 181 | return BumpAlloc.getTotalMemory() |
Ted Kremenek | f1c3881 | 2011-07-27 18:41:20 +0000 | [diff] [blame] | 182 | + llvm::capacity_in_bytes(MacroDefinitions) |
| 183 | + llvm::capacity_in_bytes(PreprocessedEntities) |
| 184 | + llvm::capacity_in_bytes(LoadedPreprocessedEntities); |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 185 | } |