clang-tools  15.0.0git
CollectMacros.h
Go to the documentation of this file.
1 //===--- CollectMacros.h -----------------------------------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H
11 
12 #include "Protocol.h"
13 #include "SourceCode.h"
14 #include "index/SymbolID.h"
15 #include "clang/Lex/PPCallbacks.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include <string>
18 
19 namespace clang {
20 namespace clangd {
21 
23  // Instead of storing SourceLocation, we have to store the token range because
24  // SourceManager from preamble is not available when we build the AST.
27 };
28 
30  llvm::StringSet<> Names;
31  llvm::DenseMap<SymbolID, std::vector<MacroOccurrence>> MacroRefs;
32  // Somtimes it is not possible to compute the SymbolID for the Macro, e.g. a
33  // reference to an undefined macro. Store them separately, e.g. for semantic
34  // highlighting.
35  std::vector<MacroOccurrence> UnknownMacros;
36  // Ranges skipped by the preprocessor due to being inactive.
37  std::vector<Range> SkippedRanges;
38 };
39 
40 /// Collects macro references (e.g. definitions, expansions) in the main file.
41 /// It is used to:
42 /// - collect macros in the preamble section of the main file (in Preamble.cpp)
43 /// - collect macros after the preamble of the main file (in ParsedAST.cpp)
45 public:
46  explicit CollectMainFileMacros(const SourceManager &SM, MainFileMacros &Out)
47  : SM(SM), Out(Out) {}
48 
49  void FileChanged(SourceLocation Loc, FileChangeReason,
50  SrcMgr::CharacteristicKind, FileID) override {
51  InMainFile = isInsideMainFile(Loc, SM);
52  }
53 
54  void MacroDefined(const Token &MacroName, const MacroDirective *MD) override {
55  add(MacroName, MD->getMacroInfo(), /*IsDefinition=*/true);
56  }
57 
58  void MacroExpands(const Token &MacroName, const MacroDefinition &MD,
59  SourceRange Range, const MacroArgs *Args) override {
60  add(MacroName, MD.getMacroInfo());
61  }
62 
63  void MacroUndefined(const clang::Token &MacroName,
64  const clang::MacroDefinition &MD,
65  const clang::MacroDirective *Undef) override {
66  add(MacroName, MD.getMacroInfo());
67  }
68 
69  void Ifdef(SourceLocation Loc, const Token &MacroName,
70  const MacroDefinition &MD) override {
71  add(MacroName, MD.getMacroInfo());
72  }
73 
74  void Ifndef(SourceLocation Loc, const Token &MacroName,
75  const MacroDefinition &MD) override {
76  add(MacroName, MD.getMacroInfo());
77  }
78 
79  void Defined(const Token &MacroName, const MacroDefinition &MD,
80  SourceRange Range) override {
81  add(MacroName, MD.getMacroInfo());
82  }
83 
84  void SourceRangeSkipped(SourceRange R, SourceLocation EndifLoc) override {
85  if (!InMainFile)
86  return;
87  Position Begin = sourceLocToPosition(SM, R.getBegin());
88  Position End = sourceLocToPosition(SM, R.getEnd());
89  Out.SkippedRanges.push_back(Range{Begin, End});
90  }
91 
92 private:
93  void add(const Token &MacroNameTok, const MacroInfo *MI,
94  bool IsDefinition = false);
95  const SourceManager &SM;
96  bool InMainFile = true;
97  MainFileMacros &Out;
98 };
99 
100 /// Represents a `#pragma mark` in the main file.
101 ///
102 /// There can be at most one pragma mark per line.
103 struct PragmaMark {
105  std::string Trivia;
106 };
107 
108 /// Collect all pragma marks from the main file.
109 std::unique_ptr<PPCallbacks>
110 collectPragmaMarksCallback(const SourceManager &, std::vector<PragmaMark> &Out);
111 
112 } // namespace clangd
113 } // namespace clang
114 
115 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
SymbolID.h
clang::clangd::CollectMainFileMacros
Collects macro references (e.g.
Definition: CollectMacros.h:44
clang::clangd::CollectMainFileMacros::Ifdef
void Ifdef(SourceLocation Loc, const Token &MacroName, const MacroDefinition &MD) override
Definition: CollectMacros.h:69
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
clang::clangd::PragmaMark
Represents a #pragma mark in the main file.
Definition: CollectMacros.h:103
clang::clangd::CollectMainFileMacros::Defined
void Defined(const Token &MacroName, const MacroDefinition &MD, SourceRange Range) override
Definition: CollectMacros.h:79
clang::clangd::MainFileMacros::MacroRefs
llvm::DenseMap< SymbolID, std::vector< MacroOccurrence > > MacroRefs
Definition: CollectMacros.h:31
clang::clangd::MacroOccurrence
Definition: CollectMacros.h:22
clang::clangd::MacroOccurrence::Rng
Range Rng
Definition: CollectMacros.h:25
clang::clangd::sourceLocToPosition
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:213
clang::clangd::MainFileMacros::Names
llvm::StringSet Names
Definition: CollectMacros.h:30
Protocol.h
clang::clangd::CollectMainFileMacros::SourceRangeSkipped
void SourceRangeSkipped(SourceRange R, SourceLocation EndifLoc) override
Definition: CollectMacros.h:84
clang::clangd::Position
Definition: Protocol.h:153
clang::clangd::CollectMainFileMacros::Ifndef
void Ifndef(SourceLocation Loc, const Token &MacroName, const MacroDefinition &MD) override
Definition: CollectMacros.h:74
clang::clangd::MainFileMacros
Definition: CollectMacros.h:29
Args
llvm::json::Object Args
Definition: Trace.cpp:138
clang::clangd::collectPragmaMarksCallback
std::unique_ptr< PPCallbacks > collectPragmaMarksCallback(const SourceManager &SM, std::vector< PragmaMark > &Out)
Collect all pragma marks from the main file.
Definition: CollectMacros.cpp:56
clang::clangd::CollectMainFileMacros::CollectMainFileMacros
CollectMainFileMacros(const SourceManager &SM, MainFileMacros &Out)
Definition: CollectMacros.h:46
clang::clangd::CollectMainFileMacros::MacroDefined
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override
Definition: CollectMacros.h:54
clang::clangd::PragmaMark::Rng
Range Rng
Definition: CollectMacros.h:104
SourceCode.h
clang::clangd::MacroOccurrence::IsDefinition
bool IsDefinition
Definition: CollectMacros.h:26
clang::clangd::MainFileMacros::SkippedRanges
std::vector< Range > SkippedRanges
Definition: CollectMacros.h:37
clang::clangd::Range
Definition: Protocol.h:182
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::CollectMainFileMacros::FileChanged
void FileChanged(SourceLocation Loc, FileChangeReason, SrcMgr::CharacteristicKind, FileID) override
Definition: CollectMacros.h:49
clang::clangd::isInsideMainFile
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
Definition: SourceCode.cpp:414
clang::clangd::CollectMainFileMacros::MacroUndefined
void MacroUndefined(const clang::Token &MacroName, const clang::MacroDefinition &MD, const clang::MacroDirective *Undef) override
Definition: CollectMacros.h:63
clang::clangd::PragmaMark::Trivia
std::string Trivia
Definition: CollectMacros.h:105
clang::clangd::MainFileMacros::UnknownMacros
std::vector< MacroOccurrence > UnknownMacros
Definition: CollectMacros.h:35
Out
CompiledFragmentImpl & Out
Definition: ConfigCompile.cpp:99
clang::clangd::CollectMainFileMacros::MacroExpands
void MacroExpands(const Token &MacroName, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
Definition: CollectMacros.h:58