| 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 "AST.h" |
| 11 |
#include "clang/Basic/SourceLocation.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 |
|
| 34 |
class CollectPragmaMarks : public PPCallbacks { |
| 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 |