1 //===--- CollectMacros.cpp ---------------------------------------*- 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 #include "CollectMacros.h"
11 #include "clang/Basic/SourceLocation.h"
16 void CollectMainFileMacros::add(const Token
&MacroNameTok
, const MacroInfo
*MI
,
20 auto Loc
= MacroNameTok
.getLocation();
21 if (Loc
.isInvalid() || Loc
.isMacroID())
24 auto Name
= MacroNameTok
.getIdentifierInfo()->getName();
25 Out
.Names
.insert(Name
);
26 auto Range
= halfOpenToRange(
27 SM
, CharSourceRange::getCharRange(Loc
, MacroNameTok
.getEndLoc()));
28 if (auto SID
= getSymbolID(Name
, MI
, SM
))
29 Out
.MacroRefs
[SID
].push_back({Range
, IsDefinition
});
31 Out
.UnknownMacros
.push_back({Range
, IsDefinition
});
34 class CollectPragmaMarks
: public PPCallbacks
{
36 explicit CollectPragmaMarks(const SourceManager
&SM
,
37 std::vector
<clangd::PragmaMark
> &Out
)
40 void PragmaMark(SourceLocation Loc
, StringRef Trivia
) override
{
41 if (isInsideMainFile(Loc
, SM
)) {
42 // FIXME: This range should just cover `XX` in `#pragma mark XX` and
43 // `- XX` in `#pragma mark - XX`.
44 Position Start
= sourceLocToPosition(SM
, Loc
);
45 Position End
= {Start
.line
+ 1, 0};
46 Out
.emplace_back(clangd::PragmaMark
{{Start
, End
}, Trivia
.str()});
51 const SourceManager
&SM
;
52 std::vector
<clangd::PragmaMark
> &Out
;
55 std::unique_ptr
<PPCallbacks
>
56 collectPragmaMarksCallback(const SourceManager
&SM
,
57 std::vector
<PragmaMark
> &Out
) {
58 return std::make_unique
<CollectPragmaMarks
>(SM
, Out
);