Skip to content

Commit

Permalink
Merge branch 'llvm:main' into llvmgh-101657
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-sh authored Jan 23, 2025
2 parents 3903792 + c3dfd34 commit 8a464f3
Show file tree
Hide file tree
Showing 541 changed files with 22,990 additions and 12,039 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
prepare:
name: Prepare to build binaries
runs-on: ${{ inputs.runs-on }}
if: github.repository == 'llvm/llvm-project'
if: github.repository_owner == 'llvm'
outputs:
release-version: ${{ steps.vars.outputs.release-version }}
ref: ${{ steps.vars.outputs.ref }}
Expand Down Expand Up @@ -177,7 +177,7 @@ jobs:
build-release-package:
name: "Build Release Package"
needs: prepare
if: github.repository == 'llvm/llvm-project'
if: github.repository_owner == 'llvm'
runs-on: ${{ needs.prepare.outputs.build-runs-on }}
steps:

Expand Down Expand Up @@ -327,7 +327,7 @@ jobs:
- prepare
- build-release-package
if: >-
github.repository == 'llvm/llvm-project'
github.repository_owner == 'llvm'
runs-on: ${{ needs.prepare.outputs.test-runs-on }}
steps:
- name: Checkout Actions
Expand Down
24 changes: 24 additions & 0 deletions clang-tools-extra/clang-reorder-fields/ReorderFieldsAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ findMembersUsedInInitExpr(const CXXCtorInitializer *Initializer,
return Results;
}

/// Returns the start of the leading comments before `Loc`.
static SourceLocation getStartOfLeadingComment(SourceLocation Loc,
const SourceManager &SM,
const LangOptions &LangOpts) {
// We consider any leading comment token that is on the same line or
// indented similarly to the first comment to be part of the leading comment.
const unsigned Line = SM.getPresumedLineNumber(Loc);
const unsigned Column = SM.getPresumedColumnNumber(Loc);
std::optional<Token> Tok =
Lexer::findPreviousToken(Loc, SM, LangOpts, /*IncludeComments=*/true);
while (Tok && Tok->is(tok::comment)) {
const SourceLocation CommentLoc =
Lexer::GetBeginningOfToken(Tok->getLocation(), SM, LangOpts);
if (SM.getPresumedLineNumber(CommentLoc) != Line &&
SM.getPresumedColumnNumber(CommentLoc) != Column) {
break;
}
Loc = CommentLoc;
Tok = Lexer::findPreviousToken(Loc, SM, LangOpts, /*IncludeComments=*/true);
}
return Loc;
}

/// Returns the end of the trailing comments after `Loc`.
static SourceLocation getEndOfTrailingComment(SourceLocation Loc,
const SourceManager &SM,
Expand Down Expand Up @@ -159,6 +182,7 @@ static SourceRange getFullFieldSourceRange(const FieldDecl &Field,
if (CurrentToken->is(tok::semi))
break;
}
Begin = getStartOfLeadingComment(Begin, SM, LangOpts);
End = getEndOfTrailingComment(End, SM, LangOpts);
return SourceRange(Begin, End);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"

using namespace clang::ast_matchers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ UseIntegerSignComparisonCheck::UseIntegerSignComparisonCheck(
: ClangTidyCheck(Name, Context),
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
utils::IncludeSorter::IS_LLVM),
areDiagsSelfContained()) {}
areDiagsSelfContained()),
EnableQtSupport(Options.get("EnableQtSupport", false)) {}

void UseIntegerSignComparisonCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
Options.store(Opts, "EnableQtSupport", EnableQtSupport);
}

void UseIntegerSignComparisonCheck::registerMatchers(MatchFinder *Finder) {
Expand Down Expand Up @@ -154,8 +156,17 @@ void UseIntegerSignComparisonCheck::check(
DiagnosticBuilder Diag =
diag(BinaryOp->getBeginLoc(),
"comparison between 'signed' and 'unsigned' integers");
const std::string CmpNamespace = ("std::" + parseOpCode(OpCode)).str();
const std::string CmpHeader = "<utility>";
std::string CmpNamespace;
llvm::StringRef CmpHeader;

if (getLangOpts().CPlusPlus20) {
CmpHeader = "<utility>";
CmpNamespace = llvm::Twine("std::" + parseOpCode(OpCode)).str();
} else if (getLangOpts().CPlusPlus17 && EnableQtSupport) {
CmpHeader = "<QtCore/q20utility.h>";
CmpNamespace = llvm::Twine("q20::" + parseOpCode(OpCode)).str();
}

// Prefer modernize-use-integer-sign-comparison when C++20 is available!
Diag << FixItHint::CreateReplacement(
CharSourceRange(R1, SubExprLHS != nullptr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ class UseIntegerSignComparisonCheck : public ClangTidyCheck {
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus20;
return LangOpts.CPlusPlus20 || (LangOpts.CPlusPlus17 && EnableQtSupport);
}

private:
utils::IncludeInserter IncludeInserter;
const bool EnableQtSupport;
};

} // namespace clang::tidy::modernize
Expand Down
25 changes: 8 additions & 17 deletions clang-tools-extra/clang-tidy/utils/LexerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,16 @@ namespace clang::tidy::utils::lexer {
std::pair<Token, SourceLocation>
getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM,
const LangOptions &LangOpts, bool SkipComments) {
Token Token;
Token.setKind(tok::unknown);
const std::optional<Token> Tok =
Lexer::findPreviousToken(Location, SM, LangOpts, !SkipComments);

Location = Location.getLocWithOffset(-1);
if (Location.isInvalid())
return {Token, Location};

const auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Location));
while (Location != StartOfFile) {
Location = Lexer::GetBeginningOfToken(Location, SM, LangOpts);
if (!Lexer::getRawToken(Location, Token, SM, LangOpts) &&
(!SkipComments || !Token.is(tok::comment))) {
break;
}
if (Location == StartOfFile)
return {Token, Location};
Location = Location.getLocWithOffset(-1);
if (Tok.has_value()) {
return {*Tok, Lexer::GetBeginningOfToken(Tok->getLocation(), SM, LangOpts)};
}
return {Token, Location};

Token Token;
Token.setKind(tok::unknown);
return {Token, SourceLocation()};
}

Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
Expand Down
10 changes: 10 additions & 0 deletions clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,16 @@ bool OverlayCDB::setCompileCommand(PathRef File,
return true;
}

std::unique_ptr<ProjectModules>
OverlayCDB::getProjectModules(PathRef File) const {
auto MDB = DelegatingCDB::getProjectModules(File);
MDB->setCommandMangler([&Mangler = Mangler](tooling::CompileCommand &Command,
PathRef CommandPath) {
Mangler(Command, CommandPath);
});
return MDB;
}

DelegatingCDB::DelegatingCDB(const GlobalCompilationDatabase *Base)
: Base(Base) {
if (Base)
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clangd/GlobalCompilationDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ class OverlayCDB : public DelegatingCDB {
setCompileCommand(PathRef File,
std::optional<tooling::CompileCommand> CompilationCommand);

std::unique_ptr<ProjectModules>
getProjectModules(PathRef File) const override;

private:
mutable std::mutex Mutex;
llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
Expand Down
7 changes: 7 additions & 0 deletions clang-tools-extra/clangd/ProjectModules.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROJECTMODULES_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROJECTMODULES_H

#include "support/Function.h"
#include "support/Path.h"
#include "support/ThreadsafeFS.h"
#include "clang/Tooling/CompilationDatabase.h"

#include <memory>

Expand All @@ -36,11 +38,16 @@ namespace clangd {
/// `<primary-module-name>[:partition-name]`. So module names covers partitions.
class ProjectModules {
public:
using CommandMangler =
llvm::unique_function<void(tooling::CompileCommand &, PathRef) const>;

virtual std::vector<std::string> getRequiredModules(PathRef File) = 0;
virtual PathRef
getSourceForModuleName(llvm::StringRef ModuleName,
PathRef RequiredSrcFile = PathRef()) = 0;

virtual void setCommandMangler(CommandMangler Mangler) {}

virtual ~ProjectModules() = default;
};

Expand Down
38 changes: 23 additions & 15 deletions clang-tools-extra/clangd/ScanningProjectModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class ModuleDependencyScanner {
};

/// Scanning the single file specified by \param FilePath.
std::optional<ModuleDependencyInfo> scan(PathRef FilePath);
std::optional<ModuleDependencyInfo>
scan(PathRef FilePath, const ProjectModules::CommandMangler &Mangler);

/// Scanning every source file in the current project to get the
/// <module-name> to <module-unit-source> map.
Expand All @@ -57,7 +58,7 @@ class ModuleDependencyScanner {
/// a global module dependency scanner to monitor every file. Or we
/// can simply require the build systems (or even the end users)
/// to provide the map.
void globalScan();
void globalScan(const ProjectModules::CommandMangler &Mangler);

/// Get the source file from the module name. Note that the language
/// guarantees all the module names are unique in a valid program.
Expand All @@ -69,7 +70,9 @@ class ModuleDependencyScanner {

/// Return the direct required modules. Indirect required modules are not
/// included.
std::vector<std::string> getRequiredModules(PathRef File);
std::vector<std::string>
getRequiredModules(PathRef File,
const ProjectModules::CommandMangler &Mangler);

private:
std::shared_ptr<const clang::tooling::CompilationDatabase> CDB;
Expand All @@ -87,7 +90,8 @@ class ModuleDependencyScanner {
};

std::optional<ModuleDependencyScanner::ModuleDependencyInfo>
ModuleDependencyScanner::scan(PathRef FilePath) {
ModuleDependencyScanner::scan(PathRef FilePath,
const ProjectModules::CommandMangler &Mangler) {
auto Candidates = CDB->getCompileCommands(FilePath);
if (Candidates.empty())
return std::nullopt;
Expand All @@ -97,10 +101,8 @@ ModuleDependencyScanner::scan(PathRef FilePath) {
// DirectoryBasedGlobalCompilationDatabase::getCompileCommand.
tooling::CompileCommand Cmd = std::move(Candidates.front());

static int StaticForMainAddr; // Just an address in this process.
Cmd.CommandLine.push_back("-resource-dir=" +
CompilerInvocation::GetResourcesPath(
"clangd", (void *)&StaticForMainAddr));
if (Mangler)
Mangler(Cmd, FilePath);

using namespace clang::tooling::dependencies;

Expand Down Expand Up @@ -130,9 +132,10 @@ ModuleDependencyScanner::scan(PathRef FilePath) {
return Result;
}

void ModuleDependencyScanner::globalScan() {
void ModuleDependencyScanner::globalScan(
const ProjectModules::CommandMangler &Mangler) {
for (auto &File : CDB->getAllFiles())
scan(File);
scan(File, Mangler);

GlobalScanned = true;
}
Expand All @@ -150,9 +153,9 @@ PathRef ModuleDependencyScanner::getSourceForModuleName(
return {};
}

std::vector<std::string>
ModuleDependencyScanner::getRequiredModules(PathRef File) {
auto ScanningResult = scan(File);
std::vector<std::string> ModuleDependencyScanner::getRequiredModules(
PathRef File, const ProjectModules::CommandMangler &Mangler) {
auto ScanningResult = scan(File, Mangler);
if (!ScanningResult)
return {};

Expand All @@ -177,20 +180,25 @@ class ScanningAllProjectModules : public ProjectModules {
~ScanningAllProjectModules() override = default;

std::vector<std::string> getRequiredModules(PathRef File) override {
return Scanner.getRequiredModules(File);
return Scanner.getRequiredModules(File, Mangler);
}

void setCommandMangler(CommandMangler Mangler) override {
this->Mangler = std::move(Mangler);
}

/// RequiredSourceFile is not used intentionally. See the comments of
/// ModuleDependencyScanner for detail.
PathRef
getSourceForModuleName(llvm::StringRef ModuleName,
PathRef RequiredSourceFile = PathRef()) override {
Scanner.globalScan();
Scanner.globalScan(Mangler);
return Scanner.getSourceForModuleName(ModuleName);
}

private:
ModuleDependencyScanner Scanner;
CommandMangler Mangler;
};

std::unique_ptr<ProjectModules> scanningProjectModules(
Expand Down
44 changes: 40 additions & 4 deletions clang-tools-extra/clangd/unittests/PrerequisiteModulesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
/// code mode.
#ifndef _WIN32

#include "ModulesBuilder.h"
#include "ScanningProjectModules.h"
#include "Annotations.h"
#include "CodeComplete.h"
#include "Compiler.h"
#include "ModulesBuilder.h"
#include "ScanningProjectModules.h"
#include "TestTU.h"
#include "support/ThreadsafeFS.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
Expand Down Expand Up @@ -191,6 +192,41 @@ export module M;
EXPECT_TRUE(MInfo->canReuse(*Invocation, FS.view(TestDir)));
}

TEST_F(PrerequisiteModulesTests, ModuleWithArgumentPatch) {
MockDirectoryCompilationDatabase CDB(TestDir, FS);

CDB.ExtraClangFlags.push_back("-invalid-unknown-flag");

CDB.addFile("Dep.cppm", R"cpp(
export module Dep;
)cpp");

CDB.addFile("M.cppm", R"cpp(
export module M;
import Dep;
)cpp");

// An invalid flag will break the module compilation and the
// getRequiredModules would return an empty array
auto ProjectModules = CDB.getProjectModules(getFullPath("M.cppm"));
EXPECT_TRUE(
ProjectModules->getRequiredModules(getFullPath("M.cppm")).empty());

// Set the mangler to filter out the invalid flag
ProjectModules->setCommandMangler(
[](tooling::CompileCommand &Command, PathRef) {
auto const It =
std::find(Command.CommandLine.begin(), Command.CommandLine.end(),
"-invalid-unknown-flag");
Command.CommandLine.erase(It);
});

// And now it returns a non-empty list of required modules since the
// compilation succeeded
EXPECT_FALSE(
ProjectModules->getRequiredModules(getFullPath("M.cppm")).empty());
}

TEST_F(PrerequisiteModulesTests, ModuleWithDepTest) {
MockDirectoryCompilationDatabase CDB(TestDir, FS);

Expand Down Expand Up @@ -435,7 +471,7 @@ void func() {
/*Callback=*/nullptr);
EXPECT_TRUE(Preamble);
EXPECT_TRUE(Preamble->RequiredModules);

auto Result = codeComplete(getFullPath("Use.cpp"), Test.point(),
Preamble.get(), Use, {});
EXPECT_FALSE(Result.Completions.empty());
Expand Down Expand Up @@ -474,7 +510,7 @@ void func() {
/*Callback=*/nullptr);
EXPECT_TRUE(Preamble);
EXPECT_TRUE(Preamble->RequiredModules);

auto Result = signatureHelp(getFullPath("Use.cpp"), Test.point(),
*Preamble.get(), Use, MarkupKind::PlainText);
EXPECT_FALSE(Result.signatures.empty());
Expand Down
Loading

0 comments on commit 8a464f3

Please sign in to comment.