blob: 6303c3d629354beab5627304a8086381c5818cd3 [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
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000111void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
112 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000113 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000114 return;
115
Douglas Gregor8aaca672010-03-19 21:58:23 +0000116 if (MacroDefinition *Def = findMacroDefinition(MI))
117 PreprocessedEntities.push_back(
Chandler Carrutha88a22182011-07-14 08:20:46 +0000118 new (*this) MacroExpansion(Id.getIdentifierInfo(),
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000119 Range, Def));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000120}
121
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000122void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000123 const MacroInfo *MI) {
124 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
125 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000126 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
127 MI->getDefinitionLoc(),
128 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000129 MacroDefinitions[MI] = Def;
130 PreprocessedEntities.push_back(Def);
131}
Douglas Gregoraae92242010-03-19 21:51:54 +0000132
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000133void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000134 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 Carruth3cc331a2011-03-16 18:34:36 +0000141void PreprocessingRecord::InclusionDirective(
142 SourceLocation HashLoc,
143 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000144 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000145 bool IsAngled,
146 const FileEntry *File,
147 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000148 StringRef SearchPath,
149 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000150 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 Gregorf09b6c92010-11-01 15:03:47 +0000175 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
176 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000177 PreprocessedEntities.push_back(ID);
178}
Ted Kremenek182543a2011-07-26 21:17:24 +0000179
180size_t PreprocessingRecord::getTotalMemory() const {
181 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000182 + llvm::capacity_in_bytes(MacroDefinitions)
183 + llvm::capacity_in_bytes(PreprocessedEntities)
184 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000185}