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