blob: d02db25ae39055ceef71015aa4d1033a600241a6 [file] [log] [blame]
Douglas Gregor065f8d12010-03-18 17:52:52 +00001//===--- 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 Gregor796d76a2010-10-20 22:00:55 +000017#include "llvm/Support/ErrorHandling.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000018#include "llvm/Support/Capacity.h"
Douglas Gregor065f8d12010-03-18 17:52:52 +000019
20using namespace clang;
21
Douglas Gregoraae92242010-03-19 21:51:54 +000022ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
23
Douglas Gregorf09b6c92010-11-01 15:03:47 +000024
25InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
26 InclusionKind Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000027 StringRef FileName,
Douglas Gregorf09b6c92010-11-01 15:03:47 +000028 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 Lattner0e62c1c2011-07-23 10:55:15 +000037 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000038}
39
Douglas Gregoraae92242010-03-19 21:51:54 +000040void PreprocessingRecord::MaybeLoadPreallocatedEntities() const {
41 if (!ExternalSource || LoadedPreallocatedEntities)
42 return;
43
44 LoadedPreallocatedEntities = true;
45 ExternalSource->ReadPreprocessedEntities();
46}
47
Chandler Carrutha88a22182011-07-14 08:20:46 +000048PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroExpansions)
49 : IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000050 ExternalSource(0), LoadedPreallocatedEntities(false)
Douglas Gregoraae92242010-03-19 21:51:54 +000051{
52}
53
54PreprocessingRecord::iterator
55PreprocessingRecord::begin(bool OnlyLocalEntities) {
56 if (OnlyLocalEntities)
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000057 return iterator(this, 0);
Douglas Gregoraae92242010-03-19 21:51:54 +000058
59 MaybeLoadPreallocatedEntities();
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000060 return iterator(this, -(int)LoadedPreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000061}
62
63PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
64 if (!OnlyLocalEntities)
65 MaybeLoadPreallocatedEntities();
66
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000067 return iterator(this, PreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000068}
69
Douglas Gregor065f8d12010-03-18 17:52:52 +000070void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
71 PreprocessedEntities.push_back(Entity);
72}
73
Douglas Gregoraae92242010-03-19 21:51:54 +000074void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000075 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +000076 assert(!ExternalSource &&
77 "Preprocessing record already has an external source");
78 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +000079}
80
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000081unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
82 unsigned Result = LoadedPreprocessedEntities.size();
83 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
84 + NumEntities);
85 return Result;
86}
87
88void
89PreprocessingRecord::setLoadedPreallocatedEntity(unsigned Index,
90 PreprocessedEntity *Entity) {
91 assert(Index < LoadedPreprocessedEntities.size() &&
92 "Out-of-bounds preallocated entity");
93 LoadedPreprocessedEntities[Index] = Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +000094}
95
96void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
97 MacroDefinition *MD) {
98 MacroDefinitions[Macro] = MD;
99}
100
Douglas Gregor8aaca672010-03-19 21:58:23 +0000101MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Douglas Gregor7dc87222010-03-19 17:12:43 +0000102 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 Kyrtzidis85a14bb2011-08-18 01:05:45 +0000110void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
111 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000112 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000113 return;
114
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000115 if (MI->isBuiltinMacro())
Douglas Gregor8aaca672010-03-19 21:58:23 +0000116 PreprocessedEntities.push_back(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000117 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
118 else if (MacroDefinition *Def = findMacroDefinition(MI))
119 PreprocessedEntities.push_back(
120 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000121}
122
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000123void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000124 const MacroInfo *MI) {
125 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
126 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000127 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
128 MI->getDefinitionLoc(),
129 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000130 MacroDefinitions[MI] = Def;
131 PreprocessedEntities.push_back(Def);
132}
Douglas Gregoraae92242010-03-19 21:51:54 +0000133
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000134void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000135 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 Carruth3cc331a2011-03-16 18:34:36 +0000142void PreprocessingRecord::InclusionDirective(
143 SourceLocation HashLoc,
144 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000145 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000146 bool IsAngled,
147 const FileEntry *File,
148 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000149 StringRef SearchPath,
150 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000151 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 Gregorf09b6c92010-11-01 15:03:47 +0000176 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
177 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000178 PreprocessedEntities.push_back(ID);
179}
Ted Kremenek182543a2011-07-26 21:17:24 +0000180
181size_t PreprocessingRecord::getTotalMemory() const {
182 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000183 + llvm::capacity_in_bytes(MacroDefinitions)
184 + llvm::capacity_in_bytes(PreprocessedEntities)
185 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000186}