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

Commit bf7efa2

Browse files
committed
Explicitly link macro instantiations to macro definitions in the
preprocessing record. Use that link with clang_getCursorReferenced() and clang_getCursorDefinition() to match instantiations of a macro to the definition of the macro. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98842 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 572feb2 commit bf7efa2

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

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

+12-8
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 "llvm/ADT/DenseMap.h"
1920
#include "llvm/ADT/PointerUnion.h"
2021
#include "llvm/Support/Allocator.h"
2122
#include <vector>
@@ -34,6 +35,8 @@ void operator delete(void* ptr, clang::PreprocessingRecord& PR,
3435
unsigned) throw();
3536

3637
namespace clang {
38+
class MacroDefinition;
39+
3740
/// \brief Base class that describes a preprocessed entity, which may be a
3841
/// preprocessor directive or macro instantiation.
3942
class PreprocessedEntity {
@@ -108,22 +111,20 @@ namespace clang {
108111
/// \brief The name of the macro being instantiation.
109112
IdentifierInfo *Name;
110113

111-
/// \brief The location of the definition of the macro being instantiated.
112-
SourceLocation DefinitionLocation;
114+
/// \brief The definition of this macro.
115+
MacroDefinition *Definition;
113116

114117
public:
115118
MacroInstantiation(IdentifierInfo *Name, SourceRange Range,
116-
SourceLocation DefinitionLocation)
119+
MacroDefinition *Definition)
117120
: PreprocessedEntity(MacroInstantiationKind, Range), Name(Name),
118-
DefinitionLocation(DefinitionLocation) { }
121+
Definition(Definition) { }
119122

120123
/// \brief The name of the macro being instantiated.
121124
IdentifierInfo *getName() const { return Name; }
122125

123-
/// \brief The location of the definition of the macro being instantiated.
124-
/// FIXME: Could we just provide MacroInfo pointers instead, by teaching
125-
/// the preprocessor to hold on to them when we care to keep them around?
126-
SourceLocation getDefinitionLocation() const { return DefinitionLocation; }
126+
/// \brief The definition of the macro being instantiated.
127+
MacroDefinition *getDefinition() const { return Definition; }
127128

128129
// Implement isa/cast/dyncast/etc.
129130
static bool classof(const PreprocessedEntity *PE) {
@@ -212,6 +213,9 @@ namespace clang {
212213
/// \brief The preprocessing record this action will populate.
213214
PreprocessingRecord &Record;
214215

216+
/// \brief Mapping from MacroInfo structures to their definitions.
217+
llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
218+
215219
public:
216220
explicit PopulatePreprocessingRecord(PreprocessingRecord &Record)
217221
: Record(Record) { }

Diff for: lib/Lex/PreprocessingRecord.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ void PopulatePreprocessingRecord::MacroExpands(const Token &Id,
2626
Record.addPreprocessedEntity(
2727
new (Record) MacroInstantiation(Id.getIdentifierInfo(),
2828
Id.getLocation(),
29-
MI->getDefinitionLoc()));
29+
MacroDefinitions[MI]));
3030
}
3131

3232
void PopulatePreprocessingRecord::MacroDefined(const IdentifierInfo *II,
3333
const MacroInfo *MI) {
3434
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
35-
Record.addPreprocessedEntity(
36-
new (Record) MacroDefinition(II, MI->getDefinitionLoc(), R));
35+
MacroDefinition *Def
36+
= new (Record) MacroDefinition(II, MI->getDefinitionLoc(), R);
37+
MacroDefinitions[MI] = Def;
38+
Record.addPreprocessedEntity(Def);
3739
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ int BAR STILL_NOTHING;
3232
// CHECK: Identifier: "X" [4:22 - 4:23] preprocessing directive=
3333
// CHECK: Punctuation: "##" [4:23 - 4:25] preprocessing directive=
3434
// CHECK: Identifier: "Y" [4:25 - 4:26] preprocessing directive=
35-
// CHECK: Identifier: "NOTHING" [5:1 - 5:8] macro instantiation=NOTHING
35+
// CHECK: Identifier: "NOTHING" [5:1 - 5:8] macro instantiation=NOTHING:1:9
3636
// CHECK: Punctuation: "(" [5:8 - 5:9]
3737
// CHECK: Identifier: "more" [5:9 - 5:13]
3838
// CHECK: Punctuation: "," [5:13 - 5:14]
3939
// CHECK: Identifier: "junk" [5:14 - 5:18]
4040
// CHECK: Punctuation: ")" [5:18 - 5:19]
4141
// CHECK: Keyword: "float" [5:20 - 5:25]
42-
// CHECK: Identifier: "WIBBLE" [5:26 - 5:32] macro instantiation=WIBBLE
42+
// CHECK: Identifier: "WIBBLE" [5:26 - 5:32] macro instantiation=WIBBLE:4:9
4343
// CHECK: Punctuation: "(" [5:32 - 5:33]
4444
// CHECK: Keyword: "int" [5:33 - 5:36]
4545
// CHECK: Punctuation: "," [5:36 - 5:37]
4646
// CHECK: Keyword: "float" [5:38 - 5:43]
4747
// CHECK: Punctuation: ")" [5:43 - 5:44]
4848
// CHECK: Punctuation: ";" [5:44 - 5:45]
4949
// CHECK: Keyword: "int" [6:1 - 6:4]
50-
// CHECK: Identifier: "BAR" [6:5 - 6:8] macro instantiation=BAR
51-
// CHECK: Identifier: "STILL_NOTHING" [6:9 - 6:22] macro instantiation=STILL_NOTHING
50+
// CHECK: Identifier: "BAR" [6:5 - 6:8] macro instantiation=BAR:3:9
51+
// CHECK: Identifier: "STILL_NOTHING" [6:9 - 6:22] macro instantiation=STILL_NOTHING:2:9
5252
// CHECK: Punctuation: ";" [6:22 - 6:23]
5353
// CHECK: Punctuation: "#" [7:1 - 7:2] preprocessing directive=
5454
// CHECK: Identifier: "include" [7:2 - 7:9] preprocessing directive=

Diff for: tools/CIndex/CIndex.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,11 @@ CXCursor clang_getCursorReferenced(CXCursor C) {
17651765
return clang_getNullCursor();
17661766
}
17671767

1768+
if (C.kind == CXCursor_MacroInstantiation) {
1769+
if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
1770+
return MakeMacroDefinitionCursor(Def, CXXUnit);
1771+
}
1772+
17681773
if (!clang_isReference(C.kind))
17691774
return clang_getNullCursor();
17701775

@@ -1803,6 +1808,9 @@ CXCursor clang_getCursorDefinition(CXCursor C) {
18031808
WasReference = true;
18041809
}
18051810

1811+
if (C.kind == CXCursor_MacroInstantiation)
1812+
return clang_getCursorReferenced(C);
1813+
18061814
if (!clang_isDeclaration(C.kind))
18071815
return clang_getNullCursor();
18081816

0 commit comments

Comments
 (0)