diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/clangd/CMakeLists.txt @@ -85,6 +85,7 @@ Hover.cpp IncludeFixer.cpp InlayHints.cpp + IWYU.cpp JSONTransport.cpp PathMapping.cpp Protocol.cpp diff --git a/clang-tools-extra/clangd/Headers.h b/clang-tools-extra/clangd/Headers.h --- a/clang-tools-extra/clangd/Headers.h +++ b/clang-tools-extra/clangd/Headers.h @@ -58,6 +58,7 @@ unsigned HashOffset = 0; // Byte offset from start of file to #. int HashLine = 0; // Line number containing the directive, 0-indexed. SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; + bool Used = true; // Was something from this used in the main file? }; llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &); bool operator==(const Inclusion &LHS, const Inclusion &RHS); @@ -129,6 +130,27 @@ llvm::StringRef IncludedName, llvm::StringRef IncludedRealName); + // Captures the filename corresponding to system includes like . + // This is used in for IWYU of the standard library as we want to check + // against where the spec *says* the symbols are. + void recordSystemInclude(llvm::StringRef Spelled, + llvm::StringRef IncludedName); + + // Classifying the main-file includes as "used" or "unused" is subtle + // (consider transitive includes), so we inject the algorithm. + + // An AbstractIncludeGraph maps including files to included files. + using AbstractIncludeGraph = llvm::DenseMap>; + // A UsedFunc decides whether each file included by EntryPoint is "used", + // based on the set of files that contain some referenced symbol. + using UsedFunc = llvm::DenseMap( + const AbstractIncludeGraph &, llvm::DenseSet Referenced, + unsigned EntryPoint); + // Mark each MainFileIncludes with the decision yielded by the UsedFunc. + void markUsed(llvm::StringRef EntryPoint, + llvm::ArrayRef ReferencedFiles, + llvm::function_ref); + private: // Identifying files in a way that persists from preamble build to subsequent // builds is surprisingly hard. FileID is unavailable in InclusionDirective(), diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp --- a/clang-tools-extra/clangd/Headers.cpp +++ b/clang-tools-extra/clangd/Headers.cpp @@ -102,6 +102,18 @@ IncludeStructure *Out; }; +// Map of unquoted to quoted +const llvm::StringMap &allStandardIncludes() { + static llvm::StringMap Result = { +#define SYMBOL(Name, Namespace, Header) \ + {llvm::StringRef(#Header).ltrim('<').rtrim('>'), #Header}, +#include "CSymbolMap.inc" +#include "StdSymbolMap.inc" +#undef SYMBOL + }; + return Result; +} + } // namespace bool isLiteralInclude(llvm::StringRef Include) { @@ -204,6 +216,46 @@ return Result; } +void IncludeStructure::markUsed(llvm::StringRef EntryPoint, + llvm::ArrayRef ReferencedFiles, + llvm::function_ref Algorithm) { + auto Root = NameToIndex.find(EntryPoint); + if (Root == NameToIndex.end()) { + elog("IWYU: entrypoint {0} not found in include graph", EntryPoint); + return; + } + + llvm::DenseSet Referenced; + Referenced.reserve(ReferencedFiles.size()); + for (llvm::StringRef RefName : ReferencedFiles) { + dlog("{0} is REFERENCED", RefName); + auto It = NameToIndex.find(RefName); + if (It != NameToIndex.end()) + Referenced.insert(It->second); + } + + auto Decisions = + Algorithm(IncludeChildren, std::move(Referenced), Root->second); + auto RootChildren = IncludeChildren.find(Root->second); + assert(RootChildren != IncludeChildren.end()); + llvm::DenseMap IncludeIndex; + for (const auto Index : RootChildren->second) { + if (!RealPathNames[Index].empty()) + IncludeIndex[RealPathNames[Index]] = Index; + } + for (auto &MFI : MainFileIncludes) { + // FIXME: Skip includes that are not self-contained. + auto It = IncludeIndex.find(MFI.Resolved); + if (It != IncludeIndex.end()) { + auto DIt = Decisions.find(It->second); + if (DIt != Decisions.end()) { + MFI.Used = DIt->second; + dlog("{0} is {1}", MFI.Written, MFI.Used ? "USED" : "UNUSED"); + } + } + } +} + void IncludeInserter::addExisting(const Inclusion &Inc) { IncludedHeaders.insert(Inc.Written); if (!Inc.Resolved.empty()) diff --git a/clang-tools-extra/clangd/IWYU.h b/clang-tools-extra/clangd/IWYU.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/IWYU.h @@ -0,0 +1,60 @@ +//===--- IWYU.h - include-what-you-use analysis -----------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// This defines functionality modeled after Include-What-You-Use +/// (https://include-what-you-use.org/). The variant we implement is not +/// equivalent to the original tool since we have different design limitations +/// such as performance requirements but we are trying to make it compatible +/// with the original tool in the most popular and useful cases. +/// +/// FIXME(kirillbobyrev): Add support for IWYU pragmas. +/// FIXME(kirillbobyrev): Add support for mapping files. +/// FIXME(kirillbobyrev): Add support for standard library headers. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_IWYU_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_IWYU_H + +#include "Headers.h" +#include "ParsedAST.h" +#include "clang/Basic/SourceLocation.h" +#include "llvm/ADT/DenseSet.h" + +namespace clang { +namespace clangd { + +struct ReferencedLocations { + llvm::DenseSet AllLocations; +}; +ReferencedLocations findReferencedLocations(ParsedAST &AST); + +/// Computes all referenced files given a set of SourceLocations that were +/// extracted from symbol declarations. +llvm::DenseSet +findReferencedFiles(const llvm::DenseSet &Locs, + const SourceManager &SM); + +/// Given an include graph, computes files that are referenced directly from the +/// main file (the one where diagnostics will go to, identified by passed in +/// \p EntryPoint). +inline llvm::DenseMap +directlyReferencedFiles(const IncludeStructure::AbstractIncludeGraph &Graph, + const llvm::DenseSet &Referenced, + unsigned EntryPoint) { + llvm::DenseMap Result; + for (unsigned Inclusion : Graph.lookup(EntryPoint)) + Result.try_emplace(Inclusion, Referenced.contains(Inclusion)); + return Result; +} + +} // namespace clangd +} // namespace clang + +#endif diff --git a/clang-tools-extra/clangd/IWYU.cpp b/clang-tools-extra/clangd/IWYU.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/IWYU.cpp @@ -0,0 +1,175 @@ +//===--- IWYU.cpp -----------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "IWYU.h" +#include "support/Logger.h" +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Basic/SourceLocation.h" + +namespace clang { +namespace clangd { +namespace { + +// FIXME(kirillbobyrev): We do not handle symbols from the standard library yet. +bool inStdNamespace(const DeclContext *DC, std::string &Buf) { + const NamespaceDecl *LeadingNamespace; + unsigned NamespaceCount = 0; + for (; !DC->isTranslationUnit(); DC = DC->getParent()) { + if (!DC->isTransparentContext()) { + if (DC->isNamespace()) { + if (!NamespaceCount) + LeadingNamespace = cast(DC); + ++NamespaceCount; + } else { + return false; + } + } + } + if (!NamespaceCount) + return false; + if (NamespaceCount == 1) + return LeadingNamespace->getName().startswith("std"); + Buf = printNamespaceScope(*LeadingNamespace); + return llvm::StringRef(Buf).rtrim(":").startswith("std"); +} + +class ReferencedLocationCrawler + : public RecursiveASTVisitor { + using Base = RecursiveASTVisitor; + llvm::DenseSet Visited; + bool shouldAdd(const void *P) { return P && Visited.insert(P).second; } + + void add(const Decl *D) { + std::string Buffer; + if (!D || inStdNamespace(D->getDeclContext(), Buffer) || + !shouldAdd(D->getCanonicalDecl())) { + return; + } + // FIXME(kirillbobyrev): Should we only use the main declaration instead? + for (const Decl *Redecl : D->redecls()) { + Result.AllLocations.insert(Redecl->getLocation()); + } + } + +public: + bool VisitDeclRefExpr(DeclRefExpr *DRE) { + add(DRE->getDecl()); + add(DRE->getFoundDecl()); + return true; + } + + bool VisitMemberExpr(MemberExpr *ME) { + add(ME->getMemberDecl()); + add(ME->getFoundDecl().getDecl()); + return true; + } + + bool VisitTagType(TagType *TT) { + add(TT->getDecl()); + return true; + } + + bool VisitCXXConstructExpr(CXXConstructExpr *CCE) { + add(CCE->getConstructor()); + return true; + } + + bool VisitTemplateSpecializationType(TemplateSpecializationType *TST) { + if (shouldAdd(TST)) { + add(TST->getTemplateName().getAsTemplateDecl()); // primary template + add(TST->getAsCXXRecordDecl()); // specialization + } + return true; + } + + bool VisitTypedefType(TypedefType *TT) { + add(TT->getDecl()); + return true; + } + +#if 0 + // Consider types of any subexpression used, even if the type is not named. + // This is helpful in getFoo().bar(), where Foo must be complete. + // FIXME(kirillbobyrev): This is a tricky case that raises a question - should + // we consider implicit types to be "used"? With auto's it's not so clear. + bool VisitExpr(Expr *E) { + TraverseType(E->getType()); + return true; + } +#endif + + bool TraverseType(QualType T) { + if (shouldAdd(T.getTypePtrOrNull())) { // don't care about quals + Base::TraverseType(T); + } + return true; + } + + ReferencedLocationCrawler(ReferencedLocations &Result) : Result(Result){}; + ReferencedLocations &Result; +}; + +// Given a set of referenced FileIDs, determines all the potentially-referenced +// files and macros by traversing expansion/spelling locations of macro IDs. +// This is used to map the referenced SourceLocations onto real files. +struct ReferencedFiles { + ReferencedFiles(const SourceManager &SM) : SM(SM) {} + llvm::DenseSet Files; + llvm::DenseSet Macros; + const SourceManager &SM; + + void add(SourceLocation Loc) { add(SM.getFileID(Loc), Loc); } + + void add(FileID FID, SourceLocation Loc) { + if (FID.isInvalid()) + return; + assert(SM.isInFileID(Loc, FID)); + if (Loc.isFileID()) { + Files.insert(FID); + return; + } + // Don't process the same macro FID twice. + if (!Macros.insert(FID).second) + return; + const auto &Exp = SM.getSLocEntry(FID).getExpansion(); + add(Exp.getSpellingLoc()); + add(Exp.getExpansionLocStart()); + add(Exp.getExpansionLocEnd()); + } +}; + +} // namespace + +ReferencedLocations findReferencedLocations(ParsedAST &AST) { + ReferencedLocations Result; + ReferencedLocationCrawler C(Result); + C.TraverseAST(AST.getASTContext()); + // FIXME(kirillbobyrev): Handle macros. + return Result; +} + +llvm::DenseSet +findReferencedFiles(const llvm::DenseSet &Locs, + const SourceManager &SM) { + std::vector Sorted{Locs.begin(), Locs.end()}; + llvm::sort(Sorted); // This groups by FileID! + ReferencedFiles Result(SM); + for (auto It = Sorted.begin(); It < Sorted.end();) { + FileID FID = SM.getFileID(*It); + Result.add(FID, *It); + // Cheaply skip over all the other locations from the same FileID. + // This avoids lots of redundant Loc->File lookups for the same file. + do { + ++It; + } while (It != Sorted.end() && SM.isInFileID(*It, FID)); + } + return std::move(Result.Files); +} + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/ParsedAST.h b/clang-tools-extra/clangd/ParsedAST.h --- a/clang-tools-extra/clangd/ParsedAST.h +++ b/clang-tools-extra/clangd/ParsedAST.h @@ -83,6 +83,8 @@ return getASTContext().getLangOpts(); } + void computeUsedIncludes(); + /// This function returns top-level decls present in the main file of the AST. /// The result does not include the decls that come from the preamble. /// (These should be const, but RecursiveASTVisitor requires Decl*). diff --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp --- a/clang-tools-extra/clangd/ParsedAST.cpp +++ b/clang-tools-extra/clangd/ParsedAST.cpp @@ -18,6 +18,7 @@ #include "Features.h" #include "Headers.h" #include "HeuristicResolver.h" +#include "IWYU.h" #include "IncludeFixer.h" #include "Preamble.h" #include "SourceCode.h" @@ -601,5 +602,26 @@ return llvm::None; return llvm::StringRef(Preamble->Version); } + +void ParsedAST::computeUsedIncludes() { + const auto &SM = getSourceManager(); + + auto Refs = findReferencedLocations(*this); + auto ReferencedFileIDs = + findReferencedFiles(Refs.AllLocations, getSourceManager()); + std::vector ReferencedFilenames; + ReferencedFilenames.reserve(ReferencedFileIDs.size()); + for (FileID FID : ReferencedFileIDs) { + const FileEntry *FE = SM.getFileEntryForID(FID); + if (!FE) { + elog("Missing FE for {0}", SM.getComposedLoc(FID, 0).printToString(SM)); + continue; + } + ReferencedFilenames.push_back(SM.getFileEntryForID(FID)->getName()); + } + Includes.markUsed(SM.getFileEntryForID(SM.getMainFileID())->getName(), + ReferencedFilenames, directlyReferencedFiles); +} + } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/unittests/CMakeLists.txt b/clang-tools-extra/clangd/unittests/CMakeLists.txt --- a/clang-tools-extra/clangd/unittests/CMakeLists.txt +++ b/clang-tools-extra/clangd/unittests/CMakeLists.txt @@ -61,6 +61,7 @@ IndexActionTests.cpp IndexTests.cpp InlayHintTests.cpp + IWYUTests.cpp JSONTransportTests.cpp LoggerTests.cpp LSPBinderTests.cpp diff --git a/clang-tools-extra/clangd/unittests/IWYUTests.cpp b/clang-tools-extra/clangd/unittests/IWYUTests.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clangd/unittests/IWYUTests.cpp @@ -0,0 +1,98 @@ +//===--- IWYUTests.cpp ------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "Annotations.h" +#include "IWYU.h" +#include "TestTU.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace clang { +namespace clangd { +namespace { + +using testing::ElementsAreArray; + +TEST(IWYU, ReferencedLocations) { + struct TestCase { + std::string HeaderCode; + std::string MainCode; + }; + TestCase Cases[] = { + { + "int ^x();", + "int y = x();", + }, + { + "class ^X;", + "X *y;", + }, + { + "template class ^X;", + "X *y;", + }, + { + "using ^Integer = int;", + "Integer x;", + }, + { + "struct X{int ^a;}; X ^foo();", + "int y = foo().a;", + }, + { + "class ^X{}; X ^foo();", + "auto bar() { return foo(); }", + }, + { + "class ^X; class ^X{}; class ^X;", + "X *y;", + }, + { + "struct ^X { ^X(int) {} int ^foo(); };", + "auto x = X(42); auto y = x.foo();", + }, + { + "struct ^X { static bool ^foo(); }; bool X::^foo() {}", + "auto b = X::foo();", + }, + { + "typedef bool ^Y; template struct ^X {};", + "X x;", + }, + { + // FIXME(kirillbobyrev): should report the UsingDecl. + // This information is not preserved in the AST. + // ^ + "namespace ns { class ^X; }; using ns::X;", + "X *y;", + }}; + for (const TestCase &T : Cases) { + TestTU TU; + TU.Code = T.MainCode; + Annotations Header(T.HeaderCode); + TU.HeaderCode = Header.code().str(); + auto AST = TU.build(); + + std::vector Points; + for (const auto &Loc : findReferencedLocations(AST).AllLocations) { + if (AST.getSourceManager().getBufferName(Loc).endswith( + TU.HeaderFilename)) { + Points.push_back(offsetToPosition( + TU.HeaderCode, AST.getSourceManager().getFileOffset(Loc))); + } + } + llvm::sort(Points); + + EXPECT_EQ(Points, Header.points()) << T.HeaderCode << "\n---\n" + << T.MainCode; + } +} + +} // namespace +} // namespace clangd +} // namespace clang