[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clangd / CollectMacros.cpp
blob687f86e0a77eb088bd02dfa64b3175791c5018b5
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 //===----------------------------------------------------------------------===//
9 #include "CollectMacros.h"
10 #include "AST.h"
11 #include "clang/Basic/SourceLocation.h"
13 namespace clang {
14 namespace clangd {
16 void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,
17 bool IsDefinition) {
18 if (!InMainFile)
19 return;
20 auto Loc = MacroNameTok.getLocation();
21 if (Loc.isInvalid() || Loc.isMacroID())
22 return;
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});
30 else
31 Out.UnknownMacros.push_back({Range, IsDefinition});
34 class CollectPragmaMarks : public PPCallbacks {
35 public:
36 explicit CollectPragmaMarks(const SourceManager &SM,
37 std::vector<clangd::PragmaMark> &Out)
38 : SM(SM), Out(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()});
50 private:
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);
61 } // namespace clangd
62 } // namespace clang