clang-tools 20.0.0git
FindAllMacros.cpp
Go to the documentation of this file.
1//===-- FindAllMacros.cpp - find all macros ---------------------*- 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 "FindAllMacros.h"
10#include "HeaderMapCollector.h"
11#include "PathConfig.h"
12#include "SymbolInfo.h"
13#include "clang/Basic/FileManager.h"
14#include "clang/Basic/IdentifierTable.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Lex/MacroInfo.h"
17#include "clang/Lex/Token.h"
18#include "llvm/Support/Path.h"
19#include <optional>
20
21namespace clang {
22namespace find_all_symbols {
23
24std::optional<SymbolInfo>
25FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
26 const MacroInfo *info) {
27 std::string FilePath =
28 getIncludePath(*SM, info->getDefinitionLoc(), Collector);
29 if (FilePath.empty())
30 return std::nullopt;
31 return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
32 SymbolInfo::SymbolKind::Macro, FilePath, {});
33}
34
35void FindAllMacros::MacroDefined(const Token &MacroNameTok,
36 const MacroDirective *MD) {
37 if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo()))
38 ++FileSymbols[*Symbol].Seen;
39}
40
41void FindAllMacros::MacroUsed(const Token &Name, const MacroDefinition &MD) {
42 if (!MD || !SM->isInMainFile(SM->getExpansionLoc(Name.getLocation())))
43 return;
44 if (auto Symbol = CreateMacroSymbol(Name, MD.getMacroInfo()))
45 ++FileSymbols[*Symbol].Used;
46}
47
48void FindAllMacros::MacroExpands(const Token &MacroNameTok,
49 const MacroDefinition &MD, SourceRange Range,
50 const MacroArgs *Args) {
51 MacroUsed(MacroNameTok, MD);
52}
53
54void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
55 const MacroDefinition &MD) {
56 MacroUsed(MacroNameTok, MD);
57}
58
59void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
60 const MacroDefinition &MD) {
61 MacroUsed(MacroNameTok, MD);
62}
63
65 Reporter->reportSymbols(
66 SM->getFileEntryRefForID(SM->getMainFileID())->getName(), FileSymbols);
67 FileSymbols.clear();
68}
69
70} // namespace find_all_symbols
71} // namespace clang
llvm::SmallString< 256U > Name
CharSourceRange Range
SourceRange for the file name.
SourceLocation Loc
llvm::json::Object Args
Definition: Trace.cpp:138
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Describes a named symbol from a header.
Definition: SymbolInfo.h:26
virtual void reportSymbols(llvm::StringRef FileName, const SymbolInfo::SignalMap &Symbols)=0
std::string getIncludePath(const SourceManager &SM, SourceLocation Loc, const HeaderMapCollector *Collector)
This calculates the include path for Loc.
Definition: PathConfig.cpp:17
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//