summaryrefslogtreecommitdiffstats
path: root/lib/Lex/PreprocessingRecord.cpp
blob: 9555611dc59d91acfab27ef41a5244fe0971a88f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//===--- PreprocessingRecord.cpp - Record of Preprocessing ------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file implements the PreprocessingRecord class, which maintains a record
//  of what occurred during preprocessing, and its helpers.
//
//===----------------------------------------------------------------------===//
#include "clang/Lex/PreprocessingRecord.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Token.h"
#include "clang/Basic/IdentifierTable.h"
#include "llvm/Support/ErrorHandling.h"

using namespace clang;

ExternalPreprocessingRecordSource::~ExternalPreprocessingRecordSource() { }


InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
                                       InclusionKind Kind, 
                                       llvm::StringRef FileName, 
                                       bool InQuotes, const FileEntry *File, 
                                       SourceRange Range)
  : PreprocessingDirective(InclusionDirectiveKind, Range), 
    InQuotes(InQuotes), Kind(Kind), File(File) 
{ 
  char *Memory 
    = (char*)PPRec.Allocate(FileName.size() + 1, llvm::alignOf<char>());
  memcpy(Memory, FileName.data(), FileName.size());
  Memory[FileName.size()] = 0;
  this->FileName = llvm::StringRef(Memory, FileName.size());
}

void PreprocessingRecord::MaybeLoadPreallocatedEntities() const {
  if (!ExternalSource || LoadedPreallocatedEntities)
    return;
  
  LoadedPreallocatedEntities = true;
  ExternalSource->ReadPreprocessedEntities();
}

PreprocessingRecord::PreprocessingRecord()
  : ExternalSource(0), NumPreallocatedEntities(0), 
    LoadedPreallocatedEntities(false)
{
}

PreprocessingRecord::iterator 
PreprocessingRecord::begin(bool OnlyLocalEntities) {
  if (OnlyLocalEntities)
    return PreprocessedEntities.begin() + NumPreallocatedEntities;
  
  MaybeLoadPreallocatedEntities();
  return PreprocessedEntities.begin();
}

PreprocessingRecord::iterator PreprocessingRecord::end(bool OnlyLocalEntities) {
  if (!OnlyLocalEntities)
    MaybeLoadPreallocatedEntities();
  
  return PreprocessedEntities.end();
}

PreprocessingRecord::const_iterator 
PreprocessingRecord::begin(bool OnlyLocalEntities) const {
  if (OnlyLocalEntities)
    return PreprocessedEntities.begin() + NumPreallocatedEntities;
  
  MaybeLoadPreallocatedEntities();
  return PreprocessedEntities.begin();
}

PreprocessingRecord::const_iterator 
PreprocessingRecord::end(bool OnlyLocalEntities) const {
  if (!OnlyLocalEntities)
    MaybeLoadPreallocatedEntities();
  
  return PreprocessedEntities.end();
}

void PreprocessingRecord::addPreprocessedEntity(PreprocessedEntity *Entity) {
  PreprocessedEntities.push_back(Entity);
}

void PreprocessingRecord::SetExternalSource(
                                    ExternalPreprocessingRecordSource &Source,
                                            unsigned NumPreallocatedEntities) {
  assert(!ExternalSource &&
         "Preprocessing record already has an external source");
  ExternalSource = &Source;
  this->NumPreallocatedEntities = NumPreallocatedEntities;
  PreprocessedEntities.insert(PreprocessedEntities.begin(), 
                              NumPreallocatedEntities, 0);
}

void PreprocessingRecord::SetPreallocatedEntity(unsigned Index, 
                                                PreprocessedEntity *Entity) {
  assert(Index < NumPreallocatedEntities &&"Out-of-bounds preallocated entity");
  PreprocessedEntities[Index] = Entity;
}

void PreprocessingRecord::RegisterMacroDefinition(MacroInfo *Macro, 
                                                  MacroDefinition *MD) {
  MacroDefinitions[Macro] = MD;
}

MacroDefinition *PreprocessingRecord::findMacroDefinition(const MacroInfo *MI) {
  llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
    = MacroDefinitions.find(MI);
  if (Pos == MacroDefinitions.end())
    return 0;
  
  return Pos->second;
}

void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI) {
  if (MacroDefinition *Def = findMacroDefinition(MI))
    PreprocessedEntities.push_back(
                       new (*this) MacroInstantiation(Id.getIdentifierInfo(),
                                                      Id.getLocation(),
                                                      Def));
}

void PreprocessingRecord::MacroDefined(const Token &Id,
                                       const MacroInfo *MI) {
  SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
  MacroDefinition *Def
      = new (*this) MacroDefinition(Id.getIdentifierInfo(),
                                    MI->getDefinitionLoc(),
                                    R);
  MacroDefinitions[MI] = Def;
  PreprocessedEntities.push_back(Def);
}

void PreprocessingRecord::MacroUndefined(const Token &Id,
                                         const MacroInfo *MI) {
  llvm::DenseMap<const MacroInfo *, MacroDefinition *>::iterator Pos
    = MacroDefinitions.find(MI);
  if (Pos != MacroDefinitions.end())
    MacroDefinitions.erase(Pos);
}

void PreprocessingRecord::InclusionDirective(
    SourceLocation HashLoc,
    const clang::Token &IncludeTok,
    llvm::StringRef FileName,
    bool IsAngled,
    const FileEntry *File,
    clang::SourceLocation EndLoc,
    llvm::StringRef SearchPath,
    llvm::StringRef RelativePath) {
  InclusionDirective::InclusionKind Kind = InclusionDirective::Include;
  
  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
  case tok::pp_include: 
    Kind = InclusionDirective::Include; 
    break;
    
  case tok::pp_import: 
    Kind = InclusionDirective::Import; 
    break;
    
  case tok::pp_include_next: 
    Kind = InclusionDirective::IncludeNext; 
    break;
    
  case tok::pp___include_macros: 
    Kind = InclusionDirective::IncludeMacros;
    break;
    
  default:
    llvm_unreachable("Unknown include directive kind");
    return;
  }
  
  clang::InclusionDirective *ID
    = new (*this) clang::InclusionDirective(*this, Kind, FileName, !IsAngled, 
                                            File, SourceRange(HashLoc, EndLoc));
  PreprocessedEntities.push_back(ID);
}
OpenPOWER on IntegriCloud