blob: 8d96fb00ca7ed2507b17845083906aad2c6a133e [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
Chandler Carrutha88a22182011-07-14 08:20:46 +000040PreprocessingRecord::PreprocessingRecord(bool IncludeNestedMacroExpansions)
41 : IncludeNestedMacroExpansions(IncludeNestedMacroExpansions),
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +000042 ExternalSource(0)
Douglas Gregoraae92242010-03-19 21:51:54 +000043{
44}
45
46PreprocessingRecord::iterator
47PreprocessingRecord::begin(bool OnlyLocalEntities) {
48 if (OnlyLocalEntities)
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000049 return iterator(this, 0);
Douglas Gregoraae92242010-03-19 21:51:54 +000050
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000051 return iterator(this, -(int)LoadedPreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000052}
53
54PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000055 return iterator(this, PreprocessedEntities.size());
Douglas Gregoraae92242010-03-19 21:51:54 +000056}
57
Douglas Gregor065f8d12010-03-18 17:52:52 +000058void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
59 PreprocessedEntities.push_back(Entity);
60}
61
Douglas Gregoraae92242010-03-19 21:51:54 +000062void PreprocessingRecord::SetExternalSource(
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000063 ExternalPreprocessingRecordSource &Source) {
Douglas Gregoraae92242010-03-19 21:51:54 +000064 assert(!ExternalSource &&
65 "Preprocessing record already has an external source");
66 ExternalSource = &Source;
Douglas Gregoraae92242010-03-19 21:51:54 +000067}
68
Douglas Gregor4a9c39a2011-07-21 00:47:40 +000069unsigned PreprocessingRecord::allocateLoadedEntities(unsigned NumEntities) {
70 unsigned Result = LoadedPreprocessedEntities.size();
71 LoadedPreprocessedEntities.resize(LoadedPreprocessedEntities.size()
72 + NumEntities);
73 return Result;
74}
75
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +000076void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro,
77 PPEntityID PPID) {
78 MacroDefinitions[Macro] = PPID;
Douglas Gregoraae92242010-03-19 21:51:54 +000079}
80
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +000081/// \brief Retrieve the preprocessed entity at the given ID.
82PreprocessedEntity *PreprocessingRecord::getPreprocessedEntity(PPEntityID PPID){
83 if (PPID < 0) {
84 assert(unsigned(-PPID-1) < LoadedPreprocessedEntities.size() &&
85 "Out-of bounds loaded preprocessed entity");
86 return getLoadedPreprocessedEntity(LoadedPreprocessedEntities.size()+PPID);
87 }
88 assert(unsigned(PPID) < PreprocessedEntities.size() &&
89 "Out-of bounds local preprocessed entity");
90 return PreprocessedEntities[PPID];
91}
92
93/// \brief Retrieve the loaded preprocessed entity at the given index.
94PreprocessedEntity *
95PreprocessingRecord::getLoadedPreprocessedEntity(unsigned Index) {
96 assert(Index < LoadedPreprocessedEntities.size() &&
97 "Out-of bounds loaded preprocessed entity");
98 assert(ExternalSource && "No external source to load from");
99 PreprocessedEntity *&Entity = LoadedPreprocessedEntities[Index];
100 if (!Entity) {
101 Entity = ExternalSource->ReadPreprocessedEntity(Index);
102 if (!Entity) // Failed to load.
103 Entity = new (*this)
104 PreprocessedEntity(PreprocessedEntity::InvalidKind, SourceRange());
105 }
106 return Entity;
Douglas Gregoraae92242010-03-19 21:51:54 +0000107}
108
Douglas Gregor8aaca672010-03-19 21:58:23 +0000109MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000110 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor7dc87222010-03-19 17:12:43 +0000111 = MacroDefinitions.find(MI);
112 if (Pos == MacroDefinitions.end())
113 return 0;
114
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000115 PreprocessedEntity *Entity = getPreprocessedEntity(Pos->second);
116 if (Entity->isInvalid())
117 return 0;
118 return cast<MacroDefinition>(Entity);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000119}
120
Argyrios Kyrtzidis85a14bb2011-08-18 01:05:45 +0000121void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
122 SourceRange Range) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000123 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000124 return;
125
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000126 if (MI->isBuiltinMacro())
Douglas Gregor8aaca672010-03-19 21:58:23 +0000127 PreprocessedEntities.push_back(
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +0000128 new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
129 else if (MacroDefinition *Def = findMacroDefinition(MI))
130 PreprocessedEntities.push_back(
131 new (*this) MacroExpansion(Def, Range));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000132}
133
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000134void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000135 const MacroInfo *MI) {
136 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
137 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000138 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
139 MI->getDefinitionLoc(),
140 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000141 PreprocessedEntities.push_back(Def);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000142 MacroDefinitions[MI] = getPPEntityID(PreprocessedEntities.size()-1,
143 /*isLoaded=*/false);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000144}
Douglas Gregoraae92242010-03-19 21:51:54 +0000145
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000146void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000147 const MacroInfo *MI) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000148 llvm::DenseMap<const MacroInfo *, PPEntityID>::iterator Pos
Douglas Gregor8aaca672010-03-19 21:58:23 +0000149 = MacroDefinitions.find(MI);
150 if (Pos != MacroDefinitions.end())
151 MacroDefinitions.erase(Pos);
152}
153
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000154void PreprocessingRecord::InclusionDirective(
155 SourceLocation HashLoc,
156 const clang::Token &IncludeTok,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000157 StringRef FileName,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000158 bool IsAngled,
159 const FileEntry *File,
160 clang::SourceLocation EndLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000161 StringRef SearchPath,
162 StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000163 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
164
165 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
166 case tok::pp_include:
167 Kind = InclusionDirective::Include;
168 break;
169
170 case tok::pp_import:
171 Kind = InclusionDirective::Import;
172 break;
173
174 case tok::pp_include_next:
175 Kind = InclusionDirective::IncludeNext;
176 break;
177
178 case tok::pp___include_macros:
179 Kind = InclusionDirective::IncludeMacros;
180 break;
181
182 default:
183 llvm_unreachable("Unknown include directive kind");
184 return;
185 }
186
187 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000188 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
189 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000190 PreprocessedEntities.push_back(ID);
191}
Ted Kremenek182543a2011-07-26 21:17:24 +0000192
193size_t PreprocessingRecord::getTotalMemory() const {
194 return BumpAlloc.getTotalMemory()
Ted Kremenekf1c38812011-07-27 18:41:20 +0000195 + llvm::capacity_in_bytes(MacroDefinitions)
196 + llvm::capacity_in_bytes(PreprocessedEntities)
197 + llvm::capacity_in_bytes(LoadedPreprocessedEntities);
Ted Kremenek182543a2011-07-26 21:17:24 +0000198}