blob: 307c8aad2cb069356313b8543843298245ffb5f3 [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 "clang/Basic/IdentifierTable.h"
18#include "llvm/Support/ErrorHandling.h"
Ted Kremenekf1c38812011-07-27 18:41:20 +000019#include "llvm/Support/Capacity.h"
Douglas Gregor065f8d12010-03-18 17:52:52 +000020
21using namespace clang;
22
Douglas Gregoraae92242010-03-19 21:51:54 +000023ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }
24
Douglas Gregorf09b6c92010-11-01 15:03:47 +000025
26InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
27 InclusionKind Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000028 StringRef FileName,
Douglas Gregorf09b6c92010-11-01 15:03:47 +000029 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 Lattner0e62c1c2011-07-23 10:55:15 +000038 this->FileName = StringRef(Memory, FileName.size());
Douglas Gregorf09b6c92010-11-01 15:03:47 +000039}
40
Douglas Gregoraae92242010-03-19 21:51:54 +000041void PreprocessingRecord::MaybeLoadPreallocatedEntities() const {
42 if (!ExternalSource || LoadedPreallocatedEntities)
43 return;
44
45 LoadedPreallocatedEntities = true;
46 ExternalSource->ReadPreprocessedEntities();
47}
48
Chandler Carrutha88a22182011-07-14 08:20:46 +000049PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroExpansions)
50 : IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000051 ExternalSource(0), LoadedPreallocatedEntities(false)
Douglas Gregoraae92242010-03-19 21:51:54 +000052{
53}
54
55PreprocessingRecord::iterator
56PreprocessingRecord::begin(bool OnlyLocalEntities) {
57 if (OnlyLocalEntities)
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000058 return iterator(this, 0);
Douglas Gregoraae92242010-03-19 21:51:54 +000059
60 MaybeLoadPreallocatedEntities();
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000061 return iterator(this, -(int)LoadedPreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000062}
63
64PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
65 if (!OnlyLocalEntities)
66 MaybeLoadPreallocatedEntities();
67
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000068 return iterator(this, PreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000069}
70
Douglas Gregor065f8d12010-03-18 17:52:52 +000071void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
72 PreprocessedEntities.push_back(Entity);
73}
74
Douglas Gregoraae92242010-03-19 21:51:54 +000075void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000076 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +000077 assert(!ExternalSource &&
78 "Preprocessing record already has an external source");
79 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +000080}
81
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000082unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
83 unsigned Result = LoadedPreprocessedEntities.size();
84 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
85 + NumEntities);
86 return Result;
87}
88
89void
90PreprocessingRecord::setLoadedPreallocatedEntity(unsigned Index,
91 PreprocessedEntity *Entity) {
92 assert(Index < LoadedPreprocessedEntities.size() &&
93 "Out-of-bounds preallocated entity");
94 LoadedPreprocessedEntities[Index] = Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +000095}
96
97void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
98 MacroDefinition *MD) {
99 MacroDefinitions[Macro] = MD;
100}
101
Douglas Gregor8aaca672010-03-19 21:58:23 +0000102MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Douglas Gregor7dc87222010-03-19 17:12:43 +0000103 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
111void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000112 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000113 return;
114
Douglas Gregor8aaca672010-03-19 21:58:23 +0000115 if (MacroDefinition *Def = findMacroDefinition(MI))
116 PreprocessedEntities.push_back(
Chandler Carrutha88a22182011-07-14 08:20:46 +0000117 new (*this) MacroExpansion(Id.getIdentifierInfo(),
118 Id.getLocation(), Def));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000119}
120
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000121void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000122 const MacroInfo *MI) {
123 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
124 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000125 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
126 MI->getDefinitionLoc(),
127 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000128 MacroDefinitions[MI] = Def;
129 PreprocessedEntities.push_back(Def);
130}
Douglas Gregoraae92242010-03-19 21:51:54 +0000131
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000132void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000133 const MacroInfo *MI) {
134 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
135 = MacroDefinitions.find(MI);
136 if (Pos != MacroDefinitions.end())
137 MacroDefinitions.erase(Pos);
138}
139
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000140void PreprocessingRecord::InclusionDirective(
141 SourceLocation HashLoc,
142 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000143 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000144 bool IsAngled,
145 const FileEntry *File,
146 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000147 StringRef SearchPath,
148 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000149 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
150
151 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
152 case tok::pp_include:
153 Kind = InclusionDirective::Include;
154 break;
155
156 case tok::pp_import:
157 Kind = InclusionDirective::Import;
158 break;
159
160 case tok::pp_include_next:
161 Kind = InclusionDirective::IncludeNext;
162 break;
163
164 case tok::pp___include_macros:
165 Kind = InclusionDirective::IncludeMacros;
166 break;
167
168 default:
169 llvm_unreachable("Unknown include directive kind");
170 return;
171 }
172
173 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000174 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
175 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000176 PreprocessedEntities.push_back(ID);
177}
Ted Kremenek182543a2011-07-26 21:17:24 +0000178
179size_t PreprocessingRecord::getTotalMemory() const {
180 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000181 + llvm::capacity_in_bytes(MacroDefinitions)
182 + llvm::capacity_in_bytes(PreprocessedEntities)
183 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000184}