clang: lib/Tooling/Refactoring/Rename/USRFinder.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

21

22using namespace llvm;

23

26

27namespace {

28

29

30class NamedDeclOccurrenceFindingVisitor

32public:

33

34

35 explicit NamedDeclOccurrenceFindingVisitor(const SourceLocation Point,

36 const ASTContext &Context)

37 : RecursiveSymbolVisitor(Context.getSourceManager(),

38 Context.getLangOpts()),

39 Point(Point), Context(Context) {}

40

41 bool visitSymbolOccurrence(const NamedDecl *ND,

42 ArrayRef NameRanges) {

43 if (!ND)

44 return true;

45 for (const auto &Range : NameRanges) {

46 SourceLocation Start = Range.getBegin();

47 SourceLocation End = Range.getEnd();

48 if (!Start.isValid() || !Start.isFileID() || !End.isValid() ||

49 !End.isFileID() || !isPointWithin(Start, End))

50 return true;

51 }

52 Result = ND;

53 return false;

54 }

55

56 const NamedDecl *getNamedDecl() const { return Result; }

57

58private:

59

60 bool isPointWithin(const SourceLocation Start, const SourceLocation End) {

61

62 return Point == Start || Point == End ||

63 (Context.getSourceManager().isBeforeInTranslationUnit(Start,

64 Point) &&

65 Context.getSourceManager().isBeforeInTranslationUnit(Point, End));

66 }

67

68 const NamedDecl *Result = nullptr;

69 const SourceLocation Point;

70 const ASTContext &Context;

71};

72

73}

74

78 NamedDeclOccurrenceFindingVisitor Visitor(Point, Context);

79

80

81

82

83 for (auto *CurrDecl : Context.getTranslationUnitDecl()->decls()) {

87 SM.isBeforeInTranslationUnit(StartLoc, Point) !=

88 SM.isBeforeInTranslationUnit(EndLoc, Point))

89 Visitor.TraverseDecl(CurrDecl);

90 }

91

92 return Visitor.getNamedDecl();

93}

94

95namespace {

96

97

98

99class NamedDeclFindingVisitor

101public:

102 explicit NamedDeclFindingVisitor(StringRef Name) : Name(Name) {}

103

104

105

106 bool VisitNamedDecl(const NamedDecl *ND) {

107 if (!ND)

108 return true;

109

110 if (Name != ND->getQualifiedNameAsString() &&

111 Name != "::" + ND->getQualifiedNameAsString())

112 return true;

113 Result = ND;

114 return false;

115 }

116

117 const NamedDecl *getNamedDecl() const { return Result; }

118

119private:

120 const NamedDecl *Result = nullptr;

121 StringRef Name;

122};

123

124}

125

127 const std::string &Name) {

128 NamedDeclFindingVisitor Visitor(Name);

129 Visitor.TraverseDecl(Context.getTranslationUnitDecl());

130 return Visitor.getNamedDecl();

131}

132

135

136

138 return "";

139

140 return std::string(Buff);

141}

142

143}

144}

Defines the clang::ASTContext interface.

A wrapper class around RecursiveASTVisitor that visits each occurrences of a named symbol.

Defines the SourceManager interface.

Methods for determining the USR of a symbol at a location in source code.

Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...

Decl - This represents one declaration (or definition), e.g.

This represents a decl that may have a name.

A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...

Encodes a location in the source.

bool isValid() const

Return true if this is a valid SourceLocation object.

This class handles loading and caching of source files into memory.

bool generateUSRForDecl(const Decl *D, SmallVectorImpl< char > &Buf)

Generate a USR for a Decl, including the USR prefix.

The JSON file list parser is used to communicate input to InstallAPI.

Diagnostic wrappers for TextAPI types for error reporting.