[clang] [C++20] [Modules] Introduce a tool 'clang-named-modules-querier' and two plugins 'ClangGetUsedFilesFromModulesPlugin' and 'ClangGetDeclsInModulesPlugin' (PR #72956)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 20 22:04:19 PST 2023
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff a1de0946abe8d0195bc06651e0abe32966be47cd 05453bc0da214ad69ab94d901c997c61fae86ab6 -- clang/tools/clang-named-modules-querier/ClangNamedModulesQuerier.cpp clang/tools/clang-named-modules-querier/GetDeclsInfoToJson.h clang/tools/clang-named-modules-querier/GetUsedDeclActionPlugin.cpp clang/tools/clang-named-modules-querier/GetUsedFilesFromModulesPlugin.cpp clang/include/clang/Serialization/ASTReader.h clang/lib/Serialization/ASTReader.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h
index 9f028e59b9..39f971a98b 100644
--- a/clang/include/clang/Serialization/ASTReader.h
+++ b/clang/include/clang/Serialization/ASTReader.h
@@ -1981,10 +1981,10 @@ public:
/// lookup table as unmaterialized references.
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
- /// Return false if Name is none and Decl Context doesn't come from the reader.
+ /// Return false if Name is none and Decl Context doesn't come from the
+ /// reader.
bool FindVisibleDeclsByName(const DeclContext *DC, DeclarationName Name,
- SmallVectorImpl<NamedDecl*> &Decls);
-
+ SmallVectorImpl<NamedDecl *> &Decls);
/// Read all of the declarations lexically stored in a
/// declaration context.
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 141df3beff..40f5b11fb6 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -7930,7 +7930,7 @@ void ASTReader::FindFileRegionDecls(FileID File,
bool ASTReader::FindVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name,
- SmallVectorImpl<NamedDecl*> &Decls) {
+ SmallVectorImpl<NamedDecl *> &Decls) {
if (!Name)
return false;
@@ -7951,9 +7951,8 @@ bool ASTReader::FindVisibleDeclsByName(const DeclContext *DC,
return true;
}
-bool
-ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
- DeclarationName Name) {
+bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
+ DeclarationName Name) {
assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
"DeclContext has no visible decls in storage");
diff --git a/clang/tools/clang-named-modules-querier/ClangNamedModulesQuerier.cpp b/clang/tools/clang-named-modules-querier/ClangNamedModulesQuerier.cpp
index 13503578c2..487872e609 100644
--- a/clang/tools/clang-named-modules-querier/ClangNamedModulesQuerier.cpp
+++ b/clang/tools/clang-named-modules-querier/ClangNamedModulesQuerier.cpp
@@ -8,15 +8,15 @@
#include "GetDeclsInfoToJson.h"
-#include "clang/Frontend/CompilerInstance.h"
#include "clang/AST/ASTConsumer.h"
+#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/StringMap.h"
-#include "llvm/Support/LLVMDriver.h"
#include "llvm/Support/JSON.h"
+#include "llvm/Support/LLVMDriver.h"
using namespace clang;
@@ -25,8 +25,8 @@ class DeclsQueryAction : public ASTFrontendAction {
llvm::json::Array JsonOutput;
public:
- DeclsQueryAction(std::vector<std::string> &&QueryingDeclNames) :
- QueryingDeclNames(QueryingDeclNames) {}
+ DeclsQueryAction(std::vector<std::string> &&QueryingDeclNames)
+ : QueryingDeclNames(QueryingDeclNames) {}
bool BeginInvocation(CompilerInstance &CI) override {
CI.getHeaderSearchOpts().ModuleFormat = "raw";
@@ -34,7 +34,8 @@ public:
}
DeclContext *getDeclContextByName(ASTReader *Reader, StringRef Name);
- std::optional<SmallVector<NamedDecl *>> getDeclsByName(ASTReader *Reader, StringRef Name);
+ std::optional<SmallVector<NamedDecl *>> getDeclsByName(ASTReader *Reader,
+ StringRef Name);
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
@@ -47,7 +48,8 @@ public:
assert(isCurrentFileAST() && "dumping non-AST?");
ASTReader *Reader = getCurrentASTUnit().getASTReader().get();
- serialization::ModuleFile &MF = Reader->getModuleManager().getPrimaryModule();
+ serialization::ModuleFile &MF =
+ Reader->getModuleManager().getPrimaryModule();
if (!MF.StandardCXXModule) {
llvm::errs() << "We should only consider standard C++20 Modules.\n";
return;
@@ -57,7 +59,8 @@ public:
QueryDecls(Reader, Name);
CompilerInstance &CI = getCompilerInstance();
- std::unique_ptr<raw_pwrite_stream> OS = CI.createDefaultOutputFile(/*Binary=*/false);
+ std::unique_ptr<raw_pwrite_stream> OS =
+ CI.createDefaultOutputFile(/*Binary=*/false);
if (!OS) {
llvm::errs() << "Failed to create output file\n";
return;
@@ -75,7 +78,8 @@ static DeclContext *getDeclContext(NamedDecl *ND) {
return dyn_cast<DeclContext>(ND);
}
-static DeclContext *getDeclContextFor(const SmallVector<NamedDecl *> &DCCandidates) {
+static DeclContext *
+getDeclContextFor(const SmallVector<NamedDecl *> &DCCandidates) {
DeclContext *Result = nullptr;
for (auto *ND : DCCandidates) {
@@ -97,18 +101,21 @@ static DeclContext *getDeclContextFor(const SmallVector<NamedDecl *> &DCCandidat
return Result;
}
-DeclContext *DeclsQueryAction::getDeclContextByName(ASTReader *Reader, StringRef Name) {
+DeclContext *DeclsQueryAction::getDeclContextByName(ASTReader *Reader,
+ StringRef Name) {
if (Name.empty())
return Reader->getContext().getTranslationUnitDecl();
- std::optional<SmallVector<NamedDecl *>> DCCandidates = getDeclsByName(Reader, Name);
+ std::optional<SmallVector<NamedDecl *>> DCCandidates =
+ getDeclsByName(Reader, Name);
if (!DCCandidates || DCCandidates->empty())
return nullptr;
return getDeclContextFor(*DCCandidates);
}
-std::optional<SmallVector<NamedDecl *>> DeclsQueryAction::getDeclsByName(ASTReader *Reader, StringRef Name) {
+std::optional<SmallVector<NamedDecl *>>
+DeclsQueryAction::getDeclsByName(ASTReader *Reader, StringRef Name) {
if (Name.endswith("::"))
return std::nullopt;
@@ -154,17 +161,18 @@ void DeclsQueryAction::QueryDecls(ASTReader *Reader, StringRef Name) {
// TODO: Print --help information
// TODO: Add -resource-dir automatically
-int clang_named_modules_querier_main(int argc, char **argv, const llvm::ToolContext &) {
+int clang_named_modules_querier_main(int argc, char **argv,
+ const llvm::ToolContext &) {
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
- CompilerInstance::createDiagnostics(new DiagnosticOptions());
+ CompilerInstance::createDiagnostics(new DiagnosticOptions());
CreateInvocationOptions CIOpts;
CIOpts.Diags = Diags;
CIOpts.VFS = llvm::vfs::createPhysicalFileSystem();
llvm::ArrayRef<const char *> Args(argv, argv + argc);
if (llvm::find_if(Args, [](auto &&Arg) {
- return std::strcmp(Arg, "--help") == 0;
- }) != Args.end()) {
+ return std::strcmp(Arg, "--help") == 0;
+ }) != Args.end()) {
llvm::outs() << R"cpp(
To query the decls from module files.
@@ -184,10 +192,9 @@ line and col number and the hash value of declaration.
return 0;
}
- auto DashDashIter = llvm::find_if(Args, [](auto &&V){
- return std::strcmp(V, "--") == 0;
- });
-
+ auto DashDashIter =
+ llvm::find_if(Args, [](auto &&V) { return std::strcmp(V, "--") == 0; });
+
std::vector<std::string> QueryingDeclNames;
auto Iter = DashDashIter;
// Don't record "--".
@@ -201,8 +208,8 @@ line and col number and the hash value of declaration.
return 0;
}
- std::shared_ptr<CompilerInvocation> Invocation =
- createInvocation(llvm::ArrayRef<const char *>(argv, DashDashIter), CIOpts);
+ std::shared_ptr<CompilerInvocation> Invocation = createInvocation(
+ llvm::ArrayRef<const char *>(argv, DashDashIter), CIOpts);
CompilerInstance Instance;
Instance.setDiagnostics(Diags.get());
diff --git a/clang/tools/clang-named-modules-querier/GetDeclsInfoToJson.h b/clang/tools/clang-named-modules-querier/GetDeclsInfoToJson.h
index ff250a87c0..54bceaec33 100644
--- a/clang/tools/clang-named-modules-querier/GetDeclsInfoToJson.h
+++ b/clang/tools/clang-named-modules-querier/GetDeclsInfoToJson.h
@@ -32,17 +32,20 @@ inline unsigned getHashValue(const NamedDecl *ND) {
return Hasher.CalculateHash();
}
-inline llvm::json::Object getDeclInJson(const NamedDecl *ND, SourceManager &SMgr) {
+inline llvm::json::Object getDeclInJson(const NamedDecl *ND,
+ SourceManager &SMgr) {
llvm::json::Object DeclObject;
DeclObject.try_emplace("kind", ND->getDeclKindName());
FullSourceLoc FSL(ND->getLocation(), SMgr);
const FileEntry *FE = SMgr.getFileEntryForID(FSL.getFileID());
- DeclObject.try_emplace("source File Name", FE ? FE->getName() : "Unknown Source File");
+ DeclObject.try_emplace("source File Name",
+ FE ? FE->getName() : "Unknown Source File");
DeclObject.try_emplace("line", FSL.getSpellingLineNumber());
DeclObject.try_emplace("col", FSL.getSpellingColumnNumber());
DeclObject.try_emplace("Hash", getHashValue(ND));
- return llvm::json::Object({{ND->getQualifiedNameAsString(), std::move(DeclObject)}});
-}
+ return llvm::json::Object(
+ {{ND->getQualifiedNameAsString(), std::move(DeclObject)}});
}
+} // namespace clang
#endif
diff --git a/clang/tools/clang-named-modules-querier/GetUsedDeclActionPlugin.cpp b/clang/tools/clang-named-modules-querier/GetUsedDeclActionPlugin.cpp
index a66eecbb94..6cf7d7cb78 100644
--- a/clang/tools/clang-named-modules-querier/GetUsedDeclActionPlugin.cpp
+++ b/clang/tools/clang-named-modules-querier/GetUsedDeclActionPlugin.cpp
@@ -1,4 +1,5 @@
-//===- GetUsedDeclActionPlugin.cpp -----------------------------------------===//
+//===- GetUsedDeclActionPlugin.cpp
+//-----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -8,10 +9,10 @@
#include "GetDeclsInfoToJson.h"
-#include "clang/Frontend/FrontendPluginRegistry.h"
-#include "clang/Frontend/CompilerInstance.h"
#include "clang/AST/ASTConsumer.h"
+#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "llvm/ADT/StringMap.h"
@@ -23,27 +24,32 @@ namespace clang {
class DeclsQuerier : public ASTDeserializationListener {
public:
void DeclRead(serialization::DeclID ID, const Decl *D) override {
- // We only cares about function decls, var decls, tag decls (class, struct, enum, union).
+ // We only cares about function decls, var decls, tag decls (class, struct,
+ // enum, union).
if (!isa<NamedDecl>(D))
return;
-
- // We only records the template declaration if the declaration is placed in templates.
- if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->getDescribedFunctionTemplate())
+
+ // We only records the template declaration if the declaration is placed in
+ // templates.
+ if (auto *FD = dyn_cast<FunctionDecl>(D);
+ FD && FD->getDescribedFunctionTemplate())
return;
if (auto *VD = dyn_cast<VarDecl>(D); VD && VD->getDescribedVarTemplate())
return;
- if (auto *CRD = dyn_cast<CXXRecordDecl>(D); CRD && CRD->getDescribedClassTemplate())
+ if (auto *CRD = dyn_cast<CXXRecordDecl>(D);
+ CRD && CRD->getDescribedClassTemplate())
return;
- if (isa<TemplateTypeParmDecl, NonTypeTemplateParmDecl, TemplateTemplateParmDecl>(D))
+ if (isa<TemplateTypeParmDecl, NonTypeTemplateParmDecl,
+ TemplateTemplateParmDecl>(D))
return;
-
- // We don't care about declarations in function scope.
+
+ // We don't care about declarations in function scope.
if (isa<FunctionDecl>(D->getDeclContext()))
return;
-
+
// Skip implicit declarations.
if (D->isImplicit())
return;
@@ -56,8 +62,9 @@ public:
StringRef ModuleName = M->Name;
auto Iter = Names.find(ModuleName);
if (Iter == Names.end())
- Iter = Names.try_emplace(ModuleName, std::vector<const NamedDecl*>()).first;
-
+ Iter =
+ Names.try_emplace(ModuleName, std::vector<const NamedDecl *>()).first;
+
Iter->second.push_back(cast<NamedDecl>(D));
}
@@ -69,10 +76,11 @@ class DeclsQuerierConsumer : public ASTConsumer {
StringRef InFile;
std::string OutputFile;
DeclsQuerier Querier;
-
+
public:
- DeclsQuerierConsumer(CompilerInstance &CI, StringRef InFile, StringRef OutputFile)
- : CI(CI), InFile(InFile), OutputFile(OutputFile) {}
+ DeclsQuerierConsumer(CompilerInstance &CI, StringRef InFile,
+ StringRef OutputFile)
+ : CI(CI), InFile(InFile), OutputFile(OutputFile) {}
ASTDeserializationListener *GetASTDeserializationListener() override {
return &Querier;
@@ -89,7 +97,7 @@ public:
auto OS = std::make_unique<llvm::raw_fd_ostream>(OutputFile, EC);
if (EC)
return nullptr;
-
+
return OS;
}
@@ -130,7 +138,6 @@ public:
DeclsQueryAction(StringRef OutputFile) : OutputFile(OutputFile) {}
DeclsQueryAction() = default;
-
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
return std::make_unique<DeclsQuerierConsumer>(CI, InFile, OutputFile);
@@ -163,7 +170,7 @@ If you're using plugin, use -fplugin-arg-decls_query_from_modules-output=<output
to specify the output path of used decls.
)cpp";
}
-}
+} // namespace clang
static clang::FrontendPluginRegistry::Add<clang::DeclsQueryAction>
-X("decls_query_from_modules", "query used decls from modules");
+ X("decls_query_from_modules", "query used decls from modules");
diff --git a/clang/tools/clang-named-modules-querier/GetUsedFilesFromModulesPlugin.cpp b/clang/tools/clang-named-modules-querier/GetUsedFilesFromModulesPlugin.cpp
index bf244861e2..1acb018fb7 100644
--- a/clang/tools/clang-named-modules-querier/GetUsedFilesFromModulesPlugin.cpp
+++ b/clang/tools/clang-named-modules-querier/GetUsedFilesFromModulesPlugin.cpp
@@ -6,10 +6,10 @@
//
//===----------------------------------------------------------------------===//
-#include "clang/Frontend/FrontendPluginRegistry.h"
-#include "clang/Frontend/CompilerInstance.h"
#include "clang/AST/ASTConsumer.h"
+#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "llvm/ADT/StringMap.h"
@@ -24,7 +24,7 @@ public:
if (!D->getLexicalDeclContext())
return;
- if (!isa<FunctionDecl>(D) &&
+ if (!isa<FunctionDecl>(D) &&
!D->getLexicalDeclContext()->getRedeclContext()->isFileContext())
return;
@@ -45,10 +45,11 @@ class DeclsQuerierConsumer : public ASTConsumer {
StringRef InFile;
std::string OutputFile;
DeclsQuerier Querier;
-
+
public:
- DeclsQuerierConsumer(CompilerInstance &CI, StringRef InFile, StringRef OutputFile)
- : CI(CI), InFile(InFile), OutputFile(OutputFile) {}
+ DeclsQuerierConsumer(CompilerInstance &CI, StringRef InFile,
+ StringRef OutputFile)
+ : CI(CI), InFile(InFile), OutputFile(OutputFile) {}
ASTDeserializationListener *GetASTDeserializationListener() override {
return &Querier;
@@ -65,7 +66,7 @@ public:
auto OS = std::make_unique<llvm::raw_fd_ostream>(OutputFile, EC);
if (EC)
return nullptr;
-
+
return OS;
}
@@ -75,12 +76,12 @@ public:
return;
auto &SMgr = Ctx.getSourceManager();
-
+
for (const auto &FID : Querier.FileIDSet) {
const FileEntry *FE = SMgr.getFileEntryForID(FID);
if (!FE)
continue;
-
+
*OS << FE->getName() << "\n";
}
}
@@ -125,7 +126,7 @@ If you're using plugin, use -fplugin-arg-get_used_files_from_modules-output=<out
to specify the output path of used files.
)cpp";
}
-}
+} // namespace clang
static clang::FrontendPluginRegistry::Add<clang::DeclsQueryAction>
-X("get_used_files_from_modules", "get used files from modules");
+ X("get_used_files_from_modules", "get used files from modules");
``````````
</details>
https://github.com/llvm/llvm-project/pull/72956
More information about the cfe-commits
mailing list