Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 8f7c540

Browse files
committed
[libclang] Fix annotation and getting a "macro expansion" cursor
for a builtin macro expansion. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139298 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5471bc8 commit 8f7c540

File tree

6 files changed

+66
-39
lines changed

6 files changed

+66
-39
lines changed

Diff for: include/clang/Lex/PreprocessingRecord.h

+39-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "clang/Lex/PPCallbacks.h"
1818
#include "clang/Basic/SourceLocation.h"
19+
#include "clang/Basic/IdentifierTable.h"
1920
#include "llvm/ADT/DenseMap.h"
2021
#include "llvm/Support/Allocator.h"
2122
#include <vector>
@@ -110,34 +111,6 @@ namespace clang {
110111
void operator delete(void* data) throw();
111112
};
112113

113-
/// \brief Records the location of a macro expansion.
114-
class MacroExpansion : public PreprocessedEntity {
115-
/// \brief The name of the macro being expanded.
116-
IdentifierInfo *Name;
117-
118-
/// \brief The definition of this macro.
119-
MacroDefinition *Definition;
120-
121-
public:
122-
MacroExpansion(IdentifierInfo *Name, SourceRange Range,
123-
MacroDefinition *Definition)
124-
: PreprocessedEntity(MacroExpansionKind, Range), Name(Name),
125-
Definition(Definition) { }
126-
127-
/// \brief The name of the macro being expanded.
128-
IdentifierInfo *getName() const { return Name; }
129-
130-
/// \brief The definition of the macro being expanded.
131-
MacroDefinition *getDefinition() const { return Definition; }
132-
133-
// Implement isa/cast/dyncast/etc.
134-
static bool classof(const PreprocessedEntity *PE) {
135-
return PE->getKind() == MacroExpansionKind;
136-
}
137-
static bool classof(const MacroExpansion *) { return true; }
138-
139-
};
140-
141114
/// \brief Records the presence of a preprocessor directive.
142115
class PreprocessingDirective : public PreprocessedEntity {
143116
public:
@@ -178,6 +151,44 @@ namespace clang {
178151
}
179152
static bool classof(const MacroDefinition *) { return true; }
180153
};
154+
155+
/// \brief Records the location of a macro expansion.
156+
class MacroExpansion : public PreprocessedEntity {
157+
/// \brief The definition of this macro or the name of the macro if it is
158+
/// a builtin macro.
159+
llvm::PointerUnion<IdentifierInfo *, MacroDefinition *> NameOrDef;
160+
161+
public:
162+
MacroExpansion(IdentifierInfo *BuiltinName, SourceRange Range)
163+
: PreprocessedEntity(MacroExpansionKind, Range),
164+
NameOrDef(BuiltinName) { }
165+
166+
MacroExpansion(MacroDefinition *Definition, SourceRange Range)
167+
: PreprocessedEntity(MacroExpansionKind, Range),
168+
NameOrDef(Definition) { }
169+
170+
/// \brief True if it is a builtin macro.
171+
bool isBuiltinMacro() const { return NameOrDef.is<IdentifierInfo *>(); }
172+
173+
/// \brief The name of the macro being expanded.
174+
const IdentifierInfo *getName() const {
175+
if (MacroDefinition *Def = getDefinition())
176+
return Def->getName();
177+
return NameOrDef.get<IdentifierInfo*>();
178+
}
179+
180+
/// \brief The definition of the macro being expanded. May return null if
181+
/// this is a builtin macro.
182+
MacroDefinition *getDefinition() const {
183+
return NameOrDef.dyn_cast<MacroDefinition *>();
184+
}
185+
186+
// Implement isa/cast/dyncast/etc.
187+
static bool classof(const PreprocessedEntity *PE) {
188+
return PE->getKind() == MacroExpansionKind;
189+
}
190+
static bool classof(const MacroExpansion *) { return true; }
191+
};
181192

182193
/// \brief Record the location of an inclusion directive, such as an
183194
/// \c #include or \c #import statement.

Diff for: lib/Lex/PreprocessingRecord.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "clang/Lex/PreprocessingRecord.h"
1515
#include "clang/Lex/MacroInfo.h"
1616
#include "clang/Lex/Token.h"
17-
#include "clang/Basic/IdentifierTable.h"
1817
#include "llvm/Support/ErrorHandling.h"
1918
#include "llvm/Support/Capacity.h"
2019

@@ -113,10 +112,12 @@ void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
113112
if (!IncludeNestedMacroExpansions && Id.getLocation().isMacroID())
114113
return;
115114

116-
if (MacroDefinition *Def = findMacroDefinition(MI))
115+
if (MI->isBuiltinMacro())
117116
PreprocessedEntities.push_back(
118-
new (*this) MacroExpansion(Id.getIdentifierInfo(),
119-
Range, Def));
117+
new (*this) MacroExpansion(Id.getIdentifierInfo(),Range));
118+
else if (MacroDefinition *Def = findMacroDefinition(MI))
119+
PreprocessedEntities.push_back(
120+
new (*this) MacroExpansion(Def, Range));
120121
}
121122

122123
void PreprocessingRecord::MacroDefined(const Token &Id,

Diff for: lib/Serialization/ASTReader.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -1448,11 +1448,16 @@ PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) {
14481448
if (PreprocessedEntity *PE = PPRec.getLoadedPreprocessedEntity(GlobalID-1))
14491449
return PE;
14501450

1451-
MacroExpansion *ME =
1452-
new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]),
1451+
bool isBuiltin = Record[3];
1452+
MacroExpansion *ME;
1453+
if (isBuiltin)
1454+
ME = new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[4]),
14531455
SourceRange(ReadSourceLocation(F, Record[1]),
1454-
ReadSourceLocation(F, Record[2])),
1455-
getLocalMacroDefinition(F, Record[4]));
1456+
ReadSourceLocation(F, Record[2])));
1457+
else
1458+
ME = new (PPRec) MacroExpansion(getLocalMacroDefinition(F, Record[4]),
1459+
SourceRange(ReadSourceLocation(F, Record[1]),
1460+
ReadSourceLocation(F, Record[2])));
14561461
PPRec.setLoadedPreallocatedEntity(GlobalID - 1, ME);
14571462
return ME;
14581463
}

Diff for: lib/Serialization/ASTWriter.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1871,8 +1871,11 @@ void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
18711871
Record.push_back(NextPreprocessorEntityID);
18721872
AddSourceLocation(ME->getSourceRange().getBegin(), Record);
18731873
AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1874-
AddIdentifierRef(ME->getName(), Record);
1875-
Record.push_back(getMacroDefinitionID(ME->getDefinition()));
1874+
Record.push_back(ME->isBuiltinMacro());
1875+
if (ME->isBuiltinMacro())
1876+
AddIdentifierRef(ME->getName(), Record);
1877+
else
1878+
Record.push_back(getMacroDefinitionID(ME->getDefinition()));
18761879
Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
18771880
continue;
18781881
}

Diff for: test/Index/annotate-tokens-pp.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ void test() {
2828
#include "pragma-once.h"
2929
#include "guarded.h"
3030

31-
// RUN: c-index-test -test-annotate-tokens=%s:2:1:30:1 -I%S/Inputs %s | FileCheck %s
31+
const char *fname = __FILE__;
32+
33+
// RUN: c-index-test -test-annotate-tokens=%s:2:1:32:1 -I%S/Inputs %s | FileCheck %s
3234
// CHECK: Punctuation: "#" [2:1 - 2:2] preprocessing directive=
3335
// CHECK: Identifier: "define" [2:2 - 2:8] preprocessing directive=
3436
// CHECK: Identifier: "STILL_NOTHING" [2:9 - 2:22] macro definition=STILL_NOTHING
@@ -189,3 +191,4 @@ void test() {
189191
// CHECK: Punctuation: "}" [26:1 - 26:2] UnexposedStmt=
190192
// CHECK: {{28:1.*inclusion directive=pragma-once.h.*multi-include guarded}}
191193
// CHECK: {{29:1.*inclusion directive=guarded.h.*multi-include guarded}}
194+
// CHECK: Identifier: "__FILE__" [31:21 - 31:29] macro expansion=__FILE__

Diff for: test/Index/c-index-getCursor-pp.c

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ void OBSCURE(func)(int x) {
1313

1414
B(int x);
1515

16+
const char *fname = __FILE__;
17+
1618
// RUN: c-index-test -cursor-at=%s:1:11 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-1 %s
1719
// CHECK-1: macro definition=OBSCURE
1820
// RUN: c-index-test -cursor-at=%s:2:14 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-2 %s
@@ -27,6 +29,8 @@ B(int x);
2729
// CHECK-6: inclusion directive=a.h
2830
// RUN: c-index-test -cursor-at=%s:14:1 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-7 %s
2931
// CHECK-7: macro expansion=B:12:9
32+
// RUN: c-index-test -cursor-at=%s:16:25 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-8 %s
33+
// CHECK-8: macro expansion=__FILE__
3034

3135
// Same tests, but with "editing" optimizations
3236
// RUN: env CINDEXTEST_EDITING=1 c-index-test -cursor-at=%s:1:11 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-1 %s

0 commit comments

Comments
 (0)