blob: 9f93ab04502ad01757950660d9972a223a22ec09 [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"
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,
27 llvm::StringRef FileName,
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;
37 this->FileName = llvm::StringRef(Memory, FileName.size());
38}
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 Gregor998caea2011-05-06 16:33:08 +000050 ExternalSource(0), NumPreallocatedEntities(0),
Douglas Gregoraae92242010-03-19 21:51:54 +000051 LoadedPreallocatedEntities(false)
52{
53}
54
55PreprocessingRecord::iterator
56PreprocessingRecord::begin(bool OnlyLocalEntities) {
57 if (OnlyLocalEntities)
58 return PreprocessedEntities.begin() + NumPreallocatedEntities;
59
60 MaybeLoadPreallocatedEntities();
61 return PreprocessedEntities.begin();
62}
63
64PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
65 if (!OnlyLocalEntities)
66 MaybeLoadPreallocatedEntities();
67
68 return PreprocessedEntities.end();
69}
70
71PreprocessingRecord::const_iterator
72PreprocessingRecord::begin(bool OnlyLocalEntities) const {
73 if (OnlyLocalEntities)
74 return PreprocessedEntities.begin() + NumPreallocatedEntities;
75
76 MaybeLoadPreallocatedEntities();
77 return PreprocessedEntities.begin();
78}
79
80PreprocessingRecord::const_iterator
81PreprocessingRecord::end(bool OnlyLocalEntities) const {
82 if (!OnlyLocalEntities)
83 MaybeLoadPreallocatedEntities();
84
85 return PreprocessedEntities.end();
86}
87
Douglas Gregor065f8d12010-03-18 17:52:52 +000088void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
89 PreprocessedEntities.push_back(Entity);
90}
91
Douglas Gregoraae92242010-03-19 21:51:54 +000092void PreprocessingRecord::SetExternalSource(
93 ExternalPreprocessingRecordSource &Source,
94 unsigned NumPreallocatedEntities) {
95 assert(!ExternalSource &&
96 "Preprocessing record already has an external source");
97 ExternalSource = &Source;
98 this->NumPreallocatedEntities = NumPreallocatedEntities;
99 PreprocessedEntities.insert(PreprocessedEntities.begin(),
100 NumPreallocatedEntities, 0);
101}
102
103void PreprocessingRecord::SetPreallocatedEntity(unsigned Index,
104 PreprocessedEntity *Entity) {
105 assert(Index < NumPreallocatedEntities &&"Out-of-bounds preallocated entity");
106 PreprocessedEntities[Index] = Entity;
107}
108
109void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
110 MacroDefinition *MD) {
111 MacroDefinitions[Macro] = MD;
112}
113
Douglas Gregor8aaca672010-03-19 21:58:23 +0000114MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Douglas Gregor7dc87222010-03-19 17:12:43 +0000115 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
116 = MacroDefinitions.find(MI);
117 if (Pos == MacroDefinitions.end())
118 return 0;
119
120 return Pos->second;
121}
122
123void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000124 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000125 return;
126
Douglas Gregor8aaca672010-03-19 21:58:23 +0000127 if (MacroDefinition *Def = findMacroDefinition(MI))
128 PreprocessedEntities.push_back(
Chandler Carrutha88a22182011-07-14 08:20:46 +0000129 new (*this) MacroExpansion(Id.getIdentifierInfo(),
130 Id.getLocation(), Def));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000131}
132
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000133void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000134 const MacroInfo *MI) {
135 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
136 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000137 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
138 MI->getDefinitionLoc(),
139 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000140 MacroDefinitions[MI] = Def;
141 PreprocessedEntities.push_back(Def);
142}
Douglas Gregoraae92242010-03-19 21:51:54 +0000143
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000144void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000145 const MacroInfo *MI) {
146 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
147 = MacroDefinitions.find(MI);
148 if (Pos != MacroDefinitions.end())
149 MacroDefinitions.erase(Pos);
150}
151
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000152void PreprocessingRecord::InclusionDirective(
153 SourceLocation HashLoc,
154 const clang::Token &IncludeTok,
155 llvm::StringRef FileName,
156 bool IsAngled,
157 const FileEntry *File,
158 clang::SourceLocation EndLoc,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000159 llvm::StringRef SearchPath,
160 llvm::StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000161 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
162
163 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
164 case tok::pp_include:
165 Kind = InclusionDirective::Include;
166 break;
167
168 case tok::pp_import:
169 Kind = InclusionDirective::Import;
170 break;
171
172 case tok::pp_include_next:
173 Kind = InclusionDirective::IncludeNext;
174 break;
175
176 case tok::pp___include_macros:
177 Kind = InclusionDirective::IncludeMacros;
178 break;
179
180 default:
181 llvm_unreachable("Unknown include directive kind");
182 return;
183 }
184
185 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000186 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
187 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000188 PreprocessedEntities.push_back(ID);
189}