2016-05-20 16:04:36 +08:00
|
|
|
//===-- FindAllMacros.cpp - find all macros ---------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-20 16:04:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FindAllMacros.h"
|
|
|
|
#include "HeaderMapCollector.h"
|
2016-06-01 20:01:06 +08:00
|
|
|
#include "PathConfig.h"
|
2016-05-20 16:04:36 +08:00
|
|
|
#include "SymbolInfo.h"
|
2020-03-01 01:10:42 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2016-05-20 16:04:36 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2017-02-28 16:13:15 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2016-05-20 16:04:36 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
2016-06-01 01:37:38 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-05-20 16:04:36 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace find_all_symbols {
|
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
llvm::Optional<SymbolInfo>
|
|
|
|
FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
|
|
|
|
const MacroInfo *info) {
|
|
|
|
std::string FilePath =
|
|
|
|
getIncludePath(*SM, info->getDefinitionLoc(), Collector);
|
|
|
|
if (FilePath.empty())
|
|
|
|
return llvm::None;
|
|
|
|
return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
|
2017-03-09 18:47:44 +08:00
|
|
|
SymbolInfo::SymbolKind::Macro, FilePath, {});
|
2017-02-28 16:13:15 +08:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:04:36 +08:00
|
|
|
void FindAllMacros::MacroDefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) {
|
2017-02-28 16:13:15 +08:00
|
|
|
if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo()))
|
|
|
|
++FileSymbols[*Symbol].Seen;
|
|
|
|
}
|
2016-06-01 01:37:38 +08:00
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
void FindAllMacros::MacroUsed(const Token &Name, const MacroDefinition &MD) {
|
|
|
|
if (!MD || !SM->isInMainFile(SM->getExpansionLoc(Name.getLocation())))
|
|
|
|
return;
|
|
|
|
if (auto Symbol = CreateMacroSymbol(Name, MD.getMacroInfo()))
|
|
|
|
++FileSymbols[*Symbol].Used;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindAllMacros::MacroExpands(const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD, SourceRange Range,
|
|
|
|
const MacroArgs *Args) {
|
|
|
|
MacroUsed(MacroNameTok, MD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD) {
|
|
|
|
MacroUsed(MacroNameTok, MD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
|
|
|
|
const MacroDefinition &MD) {
|
|
|
|
MacroUsed(MacroNameTok, MD);
|
|
|
|
}
|
2016-05-20 16:04:36 +08:00
|
|
|
|
2017-02-28 16:13:15 +08:00
|
|
|
void FindAllMacros::EndOfMainFile() {
|
|
|
|
Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
|
|
|
|
FileSymbols);
|
|
|
|
FileSymbols.clear();
|
2016-05-20 16:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace find_all_symbols
|
|
|
|
} // namespace clang
|