clang-tools 19.0.0git
CollectMacros.cpp
Go to the documentation of this file.
1//===--- CollectMacros.cpp ---------------------------------------*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "CollectMacros.h"
10#include "AST.h"
11#include "Protocol.h"
12#include "SourceCode.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Tooling/Syntax/Tokens.h"
15#include "llvm/ADT/STLExtras.h"
16#include <cstddef>
17
18namespace clang {
19namespace clangd {
20
21Range MacroOccurrence::toRange(const SourceManager &SM) const {
22 auto MainFile = SM.getMainFileID();
23 return halfOpenToRange(
24 SM, syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM));
25}
26
27void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,
28 bool IsDefinition, bool InIfCondition) {
29 if (!InMainFile)
30 return;
31 auto Loc = MacroNameTok.getLocation();
32 if (Loc.isInvalid() || Loc.isMacroID())
33 return;
34
35 auto Name = MacroNameTok.getIdentifierInfo()->getName();
36 Out.Names.insert(Name);
37 size_t Start = SM.getFileOffset(Loc);
38 size_t End = SM.getFileOffset(MacroNameTok.getEndLoc());
39 if (auto SID = getSymbolID(Name, MI, SM))
40 Out.MacroRefs[SID].push_back({Start, End, IsDefinition, InIfCondition});
41 else
42 Out.UnknownMacros.push_back({Start, End, IsDefinition, InIfCondition});
43}
44
45void CollectMainFileMacros::FileChanged(SourceLocation Loc, FileChangeReason,
46 SrcMgr::CharacteristicKind, FileID) {
47 InMainFile = isInsideMainFile(Loc, SM);
48}
49
51 const MacroDefinition &MD,
52 SourceRange Range,
53 const MacroArgs *Args) {
54 add(MacroName, MD.getMacroInfo());
55}
56
58 const clang::MacroDefinition &MD,
59 const clang::MacroDirective *Undef) {
60 add(MacroName, MD.getMacroInfo());
61}
62
63void CollectMainFileMacros::Ifdef(SourceLocation Loc, const Token &MacroName,
64 const MacroDefinition &MD) {
65 add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
66 /*InConditionalDirective=*/true);
67}
68
69void CollectMainFileMacros::Ifndef(SourceLocation Loc, const Token &MacroName,
70 const MacroDefinition &MD) {
71 add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
72 /*InConditionalDirective=*/true);
73}
74
75void CollectMainFileMacros::Elifdef(SourceLocation Loc, const Token &MacroName,
76 const MacroDefinition &MD) {
77 add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
78 /*InConditionalDirective=*/true);
79}
80
81void CollectMainFileMacros::Elifndef(SourceLocation Loc, const Token &MacroName,
82 const MacroDefinition &MD) {
83 add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
84 /*InConditionalDirective=*/true);
85}
86
88 const MacroDefinition &MD,
89 SourceRange Range) {
90 add(MacroName, MD.getMacroInfo(), /*IsDefinition=*/false,
91 /*InConditionalDirective=*/true);
92}
93
95 SourceLocation EndifLoc) {
96 if (!InMainFile)
97 return;
98 Position Begin = sourceLocToPosition(SM, R.getBegin());
99 Position End = sourceLocToPosition(SM, R.getEnd());
100 Out.SkippedRanges.push_back(Range{Begin, End});
101}
102
104public:
105 explicit CollectPragmaMarks(const SourceManager &SM,
106 std::vector<clangd::PragmaMark> &Out)
107 : SM(SM), Out(Out) {}
108
109 void PragmaMark(SourceLocation Loc, StringRef Trivia) override {
110 if (isInsideMainFile(Loc, SM)) {
111 // FIXME: This range should just cover `XX` in `#pragma mark XX` and
112 // `- XX` in `#pragma mark - XX`.
113 Position Start = sourceLocToPosition(SM, Loc);
114 Position End = {Start.line + 1, 0};
115 Out.emplace_back(clangd::PragmaMark{{Start, End}, Trivia.str()});
116 }
117 }
118
119private:
120 const SourceManager &SM;
121 std::vector<clangd::PragmaMark> &Out;
122};
123
124std::unique_ptr<PPCallbacks>
125collectPragmaMarksCallback(const SourceManager &SM,
126 std::vector<PragmaMark> &Out) {
127 return std::make_unique<CollectPragmaMarks>(SM, Out);
128}
129
131 const MacroDirective *MD) {
132
133 if (!InMainFile)
134 return;
135 const auto *MI = MD->getMacroInfo();
136 add(MacroName, MD->getMacroInfo(), true);
137 if (MI)
138 for (const auto &Tok : MI->tokens()) {
139 auto *II = Tok.getIdentifierInfo();
140 // Could this token be a reference to a macro? (Not param to this macro).
141 if (!II || !II->hadMacroDefinition() ||
142 llvm::is_contained(MI->params(), II))
143 continue;
144 if (const MacroInfo *MI = PP.getMacroInfo(II))
145 add(Tok, MI);
146 }
147}
148
149} // namespace clangd
150} // namespace clang
llvm::SmallString< 256U > Name
CompiledFragmentImpl & Out
std::string MainFile
SourceLocation Loc
std::string MacroName
Definition: Preamble.cpp:240
llvm::json::Object Args
Definition: Trace.cpp:138
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override
void FileChanged(SourceLocation Loc, FileChangeReason, SrcMgr::CharacteristicKind, FileID) override
void MacroExpands(const Token &MacroName, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
void Elifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
void Defined(const Token &MacroName, const MacroDefinition &MD, SourceRange Range) override
void MacroUndefined(const clang::Token &MacroName, const clang::MacroDefinition &MD, const clang::MacroDirective *Undef) override
void Ifndef(SourceLocation Loc, const Token &MacroName, const MacroDefinition &MD) override
void SourceRangeSkipped(SourceRange R, SourceLocation EndifLoc) override
void Ifdef(SourceLocation Loc, const Token &MacroName, const MacroDefinition &MD) override
void Elifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
CollectPragmaMarks(const SourceManager &SM, std::vector< clangd::PragmaMark > &Out)
void PragmaMark(SourceLocation Loc, StringRef Trivia) override
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition: AST.cpp:348
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:472
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
Definition: SourceCode.cpp:423
std::unique_ptr< PPCallbacks > collectPragmaMarksCallback(const SourceManager &SM, std::vector< PragmaMark > &Out)
Collect all pragma marks from the main file.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:214
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Range toRange(const SourceManager &SM) const
std::vector< MacroOccurrence > UnknownMacros
Definition: CollectMacros.h:43
llvm::DenseMap< SymbolID, std::vector< MacroOccurrence > > MacroRefs
Definition: CollectMacros.h:39
std::vector< Range > SkippedRanges
Definition: CollectMacros.h:45
Represents a #pragma mark in the main file.
Definition: CollectMacros.h:97