1 //===--- CollectMacros.h -----------------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H
13 #include "SourceCode.h"
14 #include "index/SymbolID.h"
15 #include "clang/Lex/PPCallbacks.h"
16 #include "llvm/ADT/DenseMap.h"
22 struct MacroOccurrence
{
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.
29 struct MainFileMacros
{
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
35 std::vector
<MacroOccurrence
> UnknownMacros
;
36 // Ranges skipped by the preprocessor due to being inactive.
37 std::vector
<Range
> SkippedRanges
;
40 /// Collects macro references (e.g. definitions, expansions) in the main file.
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)
44 class CollectMainFileMacros
: public PPCallbacks
{
46 explicit CollectMainFileMacros(const SourceManager
&SM
, MainFileMacros
&Out
)
49 void FileChanged(SourceLocation Loc
, FileChangeReason
,
50 SrcMgr::CharacteristicKind
, FileID
) override
{
51 InMainFile
= isInsideMainFile(Loc
, SM
);
54 void MacroDefined(const Token
&MacroName
, const MacroDirective
*MD
) override
{
55 add(MacroName
, MD
->getMacroInfo(), /*IsDefinition=*/true);
58 void MacroExpands(const Token
&MacroName
, const MacroDefinition
&MD
,
59 SourceRange Range
, const MacroArgs
*Args
) override
{
60 add(MacroName
, MD
.getMacroInfo());
63 void MacroUndefined(const clang::Token
&MacroName
,
64 const clang::MacroDefinition
&MD
,
65 const clang::MacroDirective
*Undef
) override
{
66 add(MacroName
, MD
.getMacroInfo());
69 void Ifdef(SourceLocation Loc
, const Token
&MacroName
,
70 const MacroDefinition
&MD
) override
{
71 add(MacroName
, MD
.getMacroInfo());
74 void Ifndef(SourceLocation Loc
, const Token
&MacroName
,
75 const MacroDefinition
&MD
) override
{
76 add(MacroName
, MD
.getMacroInfo());
79 void Defined(const Token
&MacroName
, const MacroDefinition
&MD
,
80 SourceRange Range
) override
{
81 add(MacroName
, MD
.getMacroInfo());
84 void SourceRangeSkipped(SourceRange R
, SourceLocation EndifLoc
) override
{
87 Position Begin
= sourceLocToPosition(SM
, R
.getBegin());
88 Position End
= sourceLocToPosition(SM
, R
.getEnd());
89 Out
.SkippedRanges
.push_back(Range
{Begin
, End
});
93 void add(const Token
&MacroNameTok
, const MacroInfo
*MI
,
94 bool IsDefinition
= false);
95 const SourceManager
&SM
;
96 bool InMainFile
= true;
100 /// Represents a `#pragma mark` in the main file.
102 /// There can be at most one pragma mark per line.
108 /// Collect all pragma marks from the main file.
109 std::unique_ptr
<PPCallbacks
>
110 collectPragmaMarksCallback(const SourceManager
&, std::vector
<PragmaMark
> &Out
);
112 } // namespace clangd
115 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H