blob: 6e2216ae867b9e12e8c418da72234e64dc6e80c8 [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 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
110void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
Chandler Carrutha88a22182011-07-14 08:20:46 +0000111 if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
Douglas Gregor998caea2011-05-06 16:33:08 +0000112 return;
113
Douglas Gregor8aaca672010-03-19 21:58:23 +0000114 if (MacroDefinition *Def = findMacroDefinition(MI))
115 PreprocessedEntities.push_back(
Chandler Carrutha88a22182011-07-14 08:20:46 +0000116 new (*this) MacroExpansion(Id.getIdentifierInfo(),
117 Id.getLocation(), Def));
Douglas Gregor7dc87222010-03-19 17:12:43 +0000118}
119
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000120void PreprocessingRecord::MacroDefined(const Token &Id,
Douglas Gregor7dc87222010-03-19 17:12:43 +0000121 const MacroInfo *MI) {
122 SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
123 MacroDefinition *Def
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000124 = new (*this) MacroDefinition(Id.getIdentifierInfo(),
125 MI->getDefinitionLoc(),
126 R);
Douglas Gregor7dc87222010-03-19 17:12:43 +0000127 MacroDefinitions[MI] = Def;
128 PreprocessedEntities.push_back(Def);
129}
Douglas Gregoraae92242010-03-19 21:51:54 +0000130
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000131void PreprocessingRecord::MacroUndefined(const Token &Id,
Douglas Gregor8aaca672010-03-19 21:58:23 +0000132 const MacroInfo *MI) {
133 llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
134 = MacroDefinitions.find(MI);
135 if (Pos != MacroDefinitions.end())
136 MacroDefinitions.erase(Pos);
137}
138
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000139void PreprocessingRecord::InclusionDirective(
140 SourceLocation HashLoc,
141 const clang::Token &IncludeTok,
142 llvm::StringRef FileName,
143 bool IsAngled,
144 const FileEntry *File,
145 clang::SourceLocation EndLoc,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000146 llvm::StringRef SearchPath,
147 llvm::StringRef RelativePath) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000148 InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
149
150 switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
151 case tok::pp_include:
152 Kind = InclusionDirective::Include;
153 break;
154
155 case tok::pp_import:
156 Kind = InclusionDirective::Import;
157 break;
158
159 case tok::pp_include_next:
160 Kind = InclusionDirective::IncludeNext;
161 break;
162
163 case tok::pp___include_macros:
164 Kind = InclusionDirective::IncludeMacros;
165 break;
166
167 default:
168 llvm_unreachable("Unknown include directive kind");
169 return;
170 }
171
172 clang::InclusionDirective *ID
Douglas Gregorf09b6c92010-11-01 15:03:47 +0000173 = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled,
174 File, SourceRange(HashLoc, EndLoc));
Douglas Gregor796d76a2010-10-20 22:00:55 +0000175 PreprocessedEntities.push_back(ID);
176}