Skip to content

Commit

Permalink
LLVM and SPIRV-LLVM-Translator pulldown (WW42)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-sycl committed Oct 13, 2021
2 parents 49eb2d7 + cdf8bf2 commit e9b707d
Show file tree
Hide file tree
Showing 2,698 changed files with 78,525 additions and 40,429 deletions.
8 changes: 4 additions & 4 deletions clang-tools-extra/clang-tidy/add_new_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ def update_checks_list(clang_tidy_path):
def has_auto_fix(check_name):
dirname, _, check_name = check_name.partition("-")

checkerCode = get_actual_filename(dirname,
get_camel_name(check_name) + '.cpp')
checker_code = get_actual_filename(os.path.join(clang_tidy_path, dirname),
get_camel_name(check_name) + '.cpp')

if not os.path.isfile(checkerCode):
if not os.path.isfile(checker_code):
return ""

with io.open(checkerCode, encoding='utf8') as f:
with io.open(checker_code, encoding='utf8') as f:
code = f.read()
if 'FixItHint' in code or "ReplacementText" in code or "fixit" in code:
# Some simple heuristics to figure out if a checker has an autofix or not.
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/Headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class RecordHeaders : public PPCallbacks {
SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
Inc.FileKind = FileKind;
Inc.Directive = IncludeTok.getIdentifierInfo()->getPPKeywordID();
if (File)
Inc.HeaderID = static_cast<unsigned>(Out->getOrCreateID(File));
}

// Record include graph (not just for main-file includes)
Expand Down
6 changes: 1 addition & 5 deletions clang-tools-extra/clangd/Headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct Inclusion {
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;
llvm::Optional<unsigned> HeaderID;
};
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &);
bool operator==(const Inclusion &LHS, const Inclusion &RHS);
Expand Down Expand Up @@ -124,11 +125,6 @@ class IncludeStructure {
// populates the structure.
std::unique_ptr<PPCallbacks> collect(const SourceManager &SM);

void setMainFileEntry(const FileEntry *Entry) {
assert(Entry && Entry->isValid());
this->MainFileEntry = Entry;
}

// HeaderID identifies file in the include graph. It corresponds to a
// FileEntry rather than a FileID, but stays stable across preamble & main
// file builds.
Expand Down
92 changes: 92 additions & 0 deletions clang-tools-extra/clangd/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,35 @@ class ReferencedLocationCrawler
llvm::DenseSet<const void *> Visited;
};

// 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<FileID> Files;
llvm::DenseSet<FileID> 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) {
Expand All @@ -108,5 +137,68 @@ ReferencedLocations findReferencedLocations(ParsedAST &AST) {
return Result;
}

llvm::DenseSet<FileID>
findReferencedFiles(const llvm::DenseSet<SourceLocation> &Locs,
const SourceManager &SM) {
std::vector<SourceLocation> Sorted{Locs.begin(), Locs.end()};
llvm::sort(Sorted); // Group 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);
}

std::vector<const Inclusion *>
getUnused(const IncludeStructure &Structure,
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles) {
std::vector<const Inclusion *> Unused;
for (const Inclusion &MFI : Structure.MainFileIncludes) {
// FIXME: Skip includes that are not self-contained.
if (!MFI.HeaderID) {
elog("File {0} not found.", MFI.Written);
continue;
}
auto IncludeID = static_cast<IncludeStructure::HeaderID>(*MFI.HeaderID);
if (!ReferencedFiles.contains(IncludeID)) {
Unused.push_back(&MFI);
}
dlog("{0} is {1}", MFI.Written,
ReferencedFiles.contains(IncludeID) ? "USED" : "UNUSED");
}
return Unused;
}

llvm::DenseSet<IncludeStructure::HeaderID>
translateToHeaderIDs(const llvm::DenseSet<FileID> &Files,
const IncludeStructure &Includes,
const SourceManager &SM) {
llvm::DenseSet<IncludeStructure::HeaderID> TranslatedHeaderIDs;
TranslatedHeaderIDs.reserve(Files.size());
for (FileID FID : Files) {
const FileEntry *FE = SM.getFileEntryForID(FID);
assert(FE);
const auto File = Includes.getID(FE);
assert(File);
TranslatedHeaderIDs.insert(*File);
}
return TranslatedHeaderIDs;
}

std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST) {
const auto &SM = AST.getSourceManager();

auto Refs = findReferencedLocations(AST);
auto ReferencedFiles = translateToHeaderIDs(findReferencedFiles(Refs, SM),
AST.getIncludeStructure(), SM);
return getUnused(AST.getIncludeStructure(), ReferencedFiles);
}

} // namespace clangd
} // namespace clang
17 changes: 17 additions & 0 deletions clang-tools-extra/clangd/IncludeCleaner.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "ParsedAST.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/DenseSet.h"
#include <vector>

namespace clang {
namespace clangd {
Expand All @@ -46,6 +47,22 @@ using ReferencedLocations = llvm::DenseSet<SourceLocation>;
/// - err on the side of reporting all possible locations
ReferencedLocations findReferencedLocations(ParsedAST &AST);

/// Retrieves IDs of all files containing SourceLocations from \p Locs.
llvm::DenseSet<FileID> findReferencedFiles(const ReferencedLocations &Locs,
const SourceManager &SM);

/// Maps FileIDs to the internal IncludeStructure representation (HeaderIDs).
llvm::DenseSet<IncludeStructure::HeaderID>
translateToHeaderIDs(const llvm::DenseSet<FileID> &Files,
const IncludeStructure &Includes, const SourceManager &SM);

/// Retrieves headers that are referenced from the main file but not used.
std::vector<const Inclusion *>
getUnused(const IncludeStructure &Includes,
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles);

std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST);

} // namespace clangd
} // namespace clang

Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/ParsedAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,6 @@ llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
return llvm::None;
return llvm::StringRef(Preamble->Version);
}

} // namespace clangd
} // namespace clang
12 changes: 5 additions & 7 deletions clang-tools-extra/clangd/XRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1391,13 +1391,11 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
// Special case: For virtual methods, report decl/def of overrides and
// references to all overridden methods in complete type hierarchy.
if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
if (CMD->isVirtual())
if (IdentifierAtCursor && SM.getSpellingLoc(CMD->getLocation()) ==
IdentifierAtCursor->location()) {
if (auto ID = getSymbolID(CMD))
OverriddenBy.Subjects.insert(ID);
getOverriddenMethods(CMD, OverriddenMethods);
}
if (CMD->isVirtual()) {
if (auto ID = getSymbolID(CMD))
OverriddenBy.Subjects.insert(ID);
getOverriddenMethods(CMD, OverriddenMethods);
}
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace clang {
namespace clangd {
namespace {

using ::testing::UnorderedElementsAre;

TEST(IncludeCleaner, ReferencedLocations) {
struct TestCase {
std::string HeaderCode;
Expand Down Expand Up @@ -131,6 +133,40 @@ TEST(IncludeCleaner, ReferencedLocations) {
}
}

TEST(IncludeCleaner, GetUnusedHeaders) {
llvm::StringLiteral MainFile = R"cpp(
#include "a.h"
#include "b.h"
#include "dir/c.h"
#include "dir/unused.h"
#include "unused.h"
void foo() {
a();
b();
c();
})cpp";
// Build expected ast with symbols coming from headers.
TestTU TU;
TU.Filename = "foo.cpp";
TU.AdditionalFiles["foo.h"] = "void foo();";
TU.AdditionalFiles["a.h"] = "void a();";
TU.AdditionalFiles["b.h"] = "void b();";
TU.AdditionalFiles["dir/c.h"] = "void c();";
TU.AdditionalFiles["unused.h"] = "void unused();";
TU.AdditionalFiles["dir/unused.h"] = "void dirUnused();";
TU.AdditionalFiles["not_included.h"] = "void notIncluded();";
TU.ExtraArgs = {"-I" + testPath("dir")};
TU.Code = MainFile.str();
ParsedAST AST = TU.build();
auto UnusedIncludes = computeUnusedIncludes(AST);
std::vector<std::string> UnusedHeaders;
UnusedHeaders.reserve(UnusedIncludes.size());
for (const auto &Include : UnusedIncludes)
UnusedHeaders.push_back(Include->Written);
EXPECT_THAT(UnusedHeaders,
UnorderedElementsAre("\"unused.h\"", "\"dir/unused.h\""));
}

} // namespace
} // namespace clangd
} // namespace clang
18 changes: 10 additions & 8 deletions clang-tools-extra/clangd/unittests/XRefsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1780,11 +1780,13 @@ void checkFindRefs(llvm::StringRef Test, bool UseIndex = false) {
AllOf(RangeIs(R), AttrsAre(ReferencesResult::Declaration |
ReferencesResult::Definition |
ReferencesResult::Override)));
EXPECT_THAT(
findReferences(AST, T.point(), 0, UseIndex ? TU.index().get() : nullptr)
.References,
UnorderedElementsAreArray(ExpectedLocations))
<< Test;
for (const auto &P : T.points()) {
EXPECT_THAT(findReferences(AST, P, 0, UseIndex ? TU.index().get() : nullptr)
.References,
UnorderedElementsAreArray(ExpectedLocations))
<< "Failed for Refs at " << P << "\n"
<< Test;
}
}

TEST(FindReferences, WithinAST) {
Expand Down Expand Up @@ -1961,7 +1963,7 @@ TEST(FindReferences, IncludeOverrides) {
R"cpp(
class Base {
public:
virtual void $decl[[f^unc]]() = 0;
virtu^al void $decl[[f^unc]]() ^= ^0;
};
class Derived : public Base {
public:
Expand Down Expand Up @@ -1990,13 +1992,13 @@ TEST(FindReferences, RefsToBaseMethod) {
};
class Derived : public Base {
public:
void $decl[[fu^nc]]() override;
void $decl[[fu^nc]]() over^ride;
};
void test(BaseBase* BB, Base* B, Derived* D) {
// refs to overridden methods in complete type hierarchy are reported.
BB->[[func]]();
B->[[func]]();
D->[[func]]();
D->[[fu^nc]]();
})cpp";
checkFindRefs(Test, /*UseIndex=*/true);
}
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
for (const auto &Error : DiagConsumer.take()) {
ErrorText += Error.Message.Message + "\n";
}
llvm::report_fatal_error(ErrorText);
llvm::report_fatal_error(llvm::Twine(ErrorText));
}

tooling::Replacements Fixes;
Expand Down
2 changes: 1 addition & 1 deletion clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ if (CLANG_ENABLE_BOOTSTRAP)
# adding lld to clang-bootstrap-deps without having it enabled in
# LLVM_ENABLE_PROJECTS just generates a cryptic error message.
if (NOT "lld" IN_LIST LLVM_ENABLE_PROJECTS)
message(FATAL_ERROR "LLD is enabled in the boostrap build, but lld is not in LLVM_ENABLE_PROJECTS")
message(FATAL_ERROR "LLD is enabled in the bootstrap build, but lld is not in LLVM_ENABLE_PROJECTS")
endif()
add_dependencies(clang-bootstrap-deps lld)
endif()
Expand Down
4 changes: 3 additions & 1 deletion clang/cmake/caches/Fuchsia-stage2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
set(RUNTIMES_${target}_CMAKE_MODULE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
set(RUNTIMES_${target}_CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBUNWIND_ENABLE_SHARED OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "")
Expand All @@ -136,7 +137,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
set(RUNTIMES_${target}_SANITIZER_CXX_ABI_INTREE ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_TEST_COMPILER_CFLAGS "--unwindlib=libunwind -static-libgcc" CACHE STRING "")
set(RUNTIMES_${target}_SANITIZER_COMMON_TEST_TARGET_CFLAGS "--unwindlib=libunwind -static-libgcc" CACHE STRING "")
set(RUNTIMES_${target}_TSAN_TEST_TARGET_CFLAGS "--unwindlib=libunwind" CACHE STRING "")
set(RUNTIMES_${target}_TSAN_TEST_TARGET_CFLAGS "--unwindlib=libunwind -static-libgcc" CACHE STRING "")
set(RUNTIMES_${target}_LLVM_TOOLS_DIR "${CMAKE_BINARY_DIR}/bin" CACHE BOOL "")
set(RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "")

Expand Down Expand Up @@ -269,6 +270,7 @@ set(LLVM_TOOLCHAIN_TOOLS
llvm-ifs
llvm-gsymutil
llvm-lib
llvm-libtool-darwin
llvm-lipo
llvm-mt
llvm-nm
Expand Down
Loading

0 comments on commit e9b707d

Please sign in to comment.