| 1 |
//===--- CollectMacros.h -----------------------------------------*- 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 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H |
| 11 |
|
| 12 |
#include "Protocol.h" |
| 13 |
#include "SourceCode.h" |
| 14 |
#include "index/SymbolID.h" |
| 15 |
#include "clang/Lex/PPCallbacks.h" |
| 16 |
#include "llvm/ADT/DenseMap.h" |
| 17 |
#include <string> |
| 18 |
|
| 19 |
namespace clang { |
| 20 |
namespace clangd { |
| 21 |
|
| 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. |
| 25 |
Range Rng; |
| 26 |
bool IsDefinition; |
| 27 |
}; |
| 28 |
|
| 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 |
| 34 |
// highlighting. |
| 35 |
std::vector<MacroOccurrence> UnknownMacros; |
| 36 |
// Ranges skipped by the preprocessor due to being inactive. |
| 37 |
std::vector<Range> SkippedRanges; |
| 38 |
}; |
| 39 |
|
| 40 |
/// Collects macro references (e.g. definitions, expansions) in the main file. |
| 41 |
/// It is used to: |
| 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 { |
| 45 |
public: |
| 46 |
explicit CollectMainFileMacros(const SourceManager &SM, MainFileMacros &Out) |
| 47 |
: SM(SM), Out(Out) {} |
| 48 |
|
| 49 |
void FileChanged(SourceLocation Loc, FileChangeReason, |
| 50 |
SrcMgr::CharacteristicKind, FileID) override { |
| 51 |
InMainFile = isInsideMainFile(Loc, SM); |
| 52 |
} |
| 53 |
|
| 54 |
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override { |
| 55 |
add(MacroName, MD->getMacroInfo(), /*IsDefinition=*/true); |
| 56 |
} |
| 57 |
|
| 58 |
void MacroExpands(const Token &MacroName, const MacroDefinition &MD, |
| 59 |
SourceRange Range, const MacroArgs *Args) override { |
| 60 |
add(MacroName, MD.getMacroInfo()); |
| 61 |
} |
| 62 |
|
| 63 |
void MacroUndefined(const clang::Token &MacroName, |
| 64 |
const clang::MacroDefinition &MD, |
| 65 |
const clang::MacroDirective *Undef) override { |
| 66 |
add(MacroName, MD.getMacroInfo()); |
| 67 |
} |
| 68 |
|
| 69 |
void Ifdef(SourceLocation Loc, const Token &MacroName, |
| 70 |
const MacroDefinition &MD) override { |
| 71 |
add(MacroName, MD.getMacroInfo()); |
| 72 |
} |
| 73 |
|
| 74 |
void Ifndef(SourceLocation Loc, const Token &MacroName, |
| 75 |
const MacroDefinition &MD) override { |
| 76 |
add(MacroName, MD.getMacroInfo()); |
| 77 |
} |
| 78 |
|
| 79 |
void Defined(const Token &MacroName, const MacroDefinition &MD, |
| 80 |
SourceRange Range) override { |
| 81 |
add(MacroName, MD.getMacroInfo()); |
| 82 |
} |
| 83 |
|
| 84 |
void SourceRangeSkipped(SourceRange R, SourceLocation EndifLoc) override { |
| 85 |
if (!InMainFile) |
| 86 |
return; |
| 87 |
Position Begin = sourceLocToPosition(SM, R.getBegin()); |
| 88 |
Position End = sourceLocToPosition(SM, R.getEnd()); |
| 89 |
Out.SkippedRanges.push_back(Range{Begin, End}); |
| 90 |
} |
| 91 |
|
| 92 |
private: |
| 93 |
void add(const Token &MacroNameTok, const MacroInfo *MI, |
| 94 |
bool IsDefinition = false); |
| 95 |
const SourceManager &SM; |
| 96 |
bool InMainFile = true; |
| 97 |
MainFileMacros &Out; |
| 98 |
}; |
| 99 |
|
| 100 |
/// Represents a `#pragma mark` in the main file. |
| 101 |
/// |
| 102 |
/// There can be at most one pragma mark per line. |
| 103 |
struct PragmaMark { |
| 104 |
Range Rng; |
| 105 |
std::string Trivia; |
| 106 |
}; |
| 107 |
|
| 108 |
/// Collect all pragma marks from the main file. |
| 109 |
std::unique_ptr<PPCallbacks> |
| 110 |
collectPragmaMarksCallback(const SourceManager &, std::vector<PragmaMark> &Out); |
| 111 |
|
| 112 |
} // namespace clangd |
| 113 |
} // namespace clang |
| 114 |
|
| 115 |
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COLLECTMACROS_H |