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