clang-tools  14.0.0git
CollectMacros.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
8 
9 #include "CollectMacros.h"
10 #include "clang/Basic/SourceLocation.h"
11 #include "clang/Lex/Lexer.h"
12 
13 namespace clang {
14 namespace clangd {
15 
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;
23 
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});
32 }
33 
35 public:
36  explicit CollectPragmaMarks(const SourceManager &SM,
37  std::vector<clangd::PragmaMark> &Out)
38  : SM(SM), Out(Out) {}
39 
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()});
47  }
48  }
49 
50 private:
51  const SourceManager &SM;
52  std::vector<clangd::PragmaMark> &Out;
53 };
54 
55 std::unique_ptr<PPCallbacks>
56 collectPragmaMarksCallback(const SourceManager &SM,
57  std::vector<PragmaMark> &Out) {
58  return std::make_unique<CollectPragmaMarks>(SM, Out);
59 }
60 
61 } // namespace clangd
62 } // namespace clang
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::clangd::CollectPragmaMarks
Definition: CollectMacros.cpp:34
clang::clangd::PragmaMark
Represents a #pragma mark in the main file.
Definition: CollectMacros.h:105
clang::clangd::CollectPragmaMarks::PragmaMark
void PragmaMark(SourceLocation Loc, StringRef Trivia) override
Definition: CollectMacros.cpp:40
clang::clangd::CollectPragmaMarks::CollectPragmaMarks
CollectPragmaMarks(const SourceManager &SM, std::vector< clangd::PragmaMark > &Out)
Definition: CollectMacros.cpp:36
clang::clangd::MainFileMacros::MacroRefs
llvm::DenseMap< SymbolID, std::vector< MacroOccurrence > > MacroRefs
Definition: CollectMacros.h:33
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
clang::clangd::halfOpenToRange
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:466
clang::clangd::Position
Definition: Protocol.h:148
CollectMacros.h
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
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::getSymbolID
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition: AST.cpp:337
PPCallbacks
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::isInsideMainFile
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
Definition: SourceCode.cpp:417
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:108
clang::clangd::MainFileMacros::UnknownMacros
std::vector< MacroOccurrence > UnknownMacros
Definition: CollectMacros.h:37
Out
CompiledFragmentImpl & Out
Definition: ConfigCompile.cpp:100