Skip to content

Commit

Permalink
Merge from 'main' to 'sycl-web' (#5)
Browse files Browse the repository at this point in the history
Also removed the workaround for D99173, and updated vc-intrinsics to use
a version with the fix it.

  CONFLICT (content): Merge conflict in clang/lib/CodeGen/CodeGenFunction.cpp
  • Loading branch information
abhinavgaba committed Jun 18, 2021
2 parents 8e0c4b0 + 193e41c commit dbde9c1
Show file tree
Hide file tree
Showing 1,192 changed files with 37,166 additions and 10,810 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../utils/LexerUtils.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/Diagnostic.h"

namespace clang {
Expand Down Expand Up @@ -72,14 +73,14 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) {
.bind(InitFunctionCallId);
}

AST_MATCHER_FUNCTION(StatementMatcher, isInitializedFromReferenceToConst) {
AST_MATCHER_FUNCTION(StatementMatcher, initializerReturnsReferenceToConst) {
auto OldVarDeclRef =
declRefExpr(to(varDecl(hasLocalStorage()).bind(OldVarDeclId)));
return declStmt(has(varDecl(hasInitializer(
return expr(
anyOf(isConstRefReturningFunctionCall(), isConstRefReturningMethodCall(),
ignoringImpCasts(OldVarDeclRef),
ignoringImpCasts(unaryOperator(
hasOperatorName("&"), hasUnaryOperand(OldVarDeclRef))))))));
ignoringImpCasts(unaryOperator(hasOperatorName("&"),
hasUnaryOperand(OldVarDeclRef)))));
}

// This checks that the variable itself is only used as const, and also makes
Expand All @@ -106,18 +107,14 @@ static bool isInitializingVariableImmutable(const VarDecl &InitializingVar,
if (!isa<ReferenceType, PointerType>(T))
return true;

auto Matches =
match(findAll(declStmt(has(varDecl(equalsNode(&InitializingVar))))
.bind("declStmt")),
BlockStmt, Context);
// The reference or pointer is not initialized in the BlockStmt. We assume
// its pointee is not modified then.
if (Matches.empty())
// The reference or pointer is not declared and hence not initialized anywhere
// in the function. We assume its pointee is not modified then.
if (!InitializingVar.isLocalVarDecl() || !InitializingVar.hasInit()) {
return true;
}

const auto *Initialization = selectFirst<DeclStmt>("declStmt", Matches);
Matches =
match(isInitializedFromReferenceToConst(), *Initialization, Context);
auto Matches = match(initializerReturnsReferenceToConst(),
*InitializingVar.getInit(), Context);
// The reference is initialized from a free function without arguments
// returning a const reference. This is a global immutable object.
if (selectFirst<CallExpr>(InitFunctionCallId, Matches) != nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H

#include "../ClangTidyCheck.h"
#include "clang/AST/Decl.h"

namespace clang {
namespace tidy {
Expand Down
15 changes: 11 additions & 4 deletions clang-tools-extra/clangd/CompileCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include <string>
#include <vector>

namespace clang {
namespace clangd {
Expand Down Expand Up @@ -209,14 +211,20 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd) const {
Cmd = tooling::getStripPluginsAdjuster()(Cmd, "");
Cmd = tooling::getClangSyntaxOnlyAdjuster()(Cmd, "");

std::vector<std::string> ToAppend;
if (ResourceDir && !Has("-resource-dir"))
Cmd.push_back(("-resource-dir=" + *ResourceDir));
ToAppend.push_back(("-resource-dir=" + *ResourceDir));

// Don't set `-isysroot` if it is already set or if `--sysroot` is set.
// `--sysroot` is a superset of the `-isysroot` argument.
if (Sysroot && !Has("-isysroot") && !Has("--sysroot")) {
Cmd.push_back("-isysroot");
Cmd.push_back(*Sysroot);
ToAppend.push_back("-isysroot");
ToAppend.push_back(*Sysroot);
}

if (!ToAppend.empty()) {
Cmd = tooling::getInsertArgumentAdjuster(
std::move(ToAppend), tooling::ArgumentInsertPosition::END)(Cmd, "");
}

if (!Cmd.empty()) {
Expand Down Expand Up @@ -504,7 +512,6 @@ void ArgStripper::process(std::vector<std::string> &Args) const {
Args.resize(Write);
}


std::string printArgv(llvm::ArrayRef<llvm::StringRef> Args) {
std::string Buf;
llvm::raw_string_ostream OS(Buf);
Expand Down
5 changes: 4 additions & 1 deletion clang-tools-extra/clangd/ConfigCompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "llvm/Support/Regex.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
#include <algorithm>
#include <string>

namespace clang {
Expand Down Expand Up @@ -270,7 +271,9 @@ struct FragmentCompiler {
Add.push_back(std::move(*A));
Out.Apply.push_back([Add(std::move(Add))](const Params &, Config &C) {
C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
Args.insert(Args.end(), Add.begin(), Add.end());
// The point to insert at. Just append when `--` isn't present.
auto It = llvm::find(Args, "--");
Args.insert(It, Add.begin(), Add.end());
});
});
}
Expand Down
44 changes: 21 additions & 23 deletions clang-tools-extra/clangd/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ bool mentionsMainFile(const Diag &D) {
return false;
}

bool isExcluded(const Diag &D) {
bool isExcluded(unsigned DiagID) {
// clang will always fail parsing MS ASM, we don't link in desc + asm parser.
if (D.ID == clang::diag::err_msasm_unable_to_create_target ||
D.ID == clang::diag::err_msasm_unsupported_arch)
if (DiagID == clang::diag::err_msasm_unable_to_create_target ||
DiagID == clang::diag::err_msasm_unsupported_arch)
return true;
return false;
}
Expand Down Expand Up @@ -726,44 +726,42 @@ void StoreDiags::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
// Handle the new main diagnostic.
flushLastDiag();

if (Adjuster) {
LastDiag = Diag();
// FIXME: Merge with feature modules.
if (Adjuster)
DiagLevel = Adjuster(DiagLevel, Info);
if (DiagLevel == DiagnosticsEngine::Ignored) {
LastPrimaryDiagnosticWasSuppressed = true;
return;
}
}
LastPrimaryDiagnosticWasSuppressed = false;

LastDiag = Diag();
FillDiagBase(*LastDiag);
if (isExcluded(LastDiag->ID))
LastDiag->Severity = DiagnosticsEngine::Ignored;
if (DiagCB)
DiagCB(Info, *LastDiag);
// Don't bother filling in the rest if diag is going to be dropped.
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
return;

LastDiagLoc.emplace(Info.getLocation(), Info.getSourceManager());
LastDiagOriginallyError = OriginallyError;

if (!Info.getFixItHints().empty())
AddFix(true /* try to invent a message instead of repeating the diag */);
if (Fixer) {
auto ExtraFixes = Fixer(DiagLevel, Info);
auto ExtraFixes = Fixer(LastDiag->Severity, Info);
LastDiag->Fixes.insert(LastDiag->Fixes.end(), ExtraFixes.begin(),
ExtraFixes.end());
}
if (DiagCB)
DiagCB(Info, *LastDiag);
} else {
// Handle a note to an existing diagnostic.

// If a diagnostic was suppressed due to the suppression filter,
// also suppress notes associated with it.
if (LastPrimaryDiagnosticWasSuppressed) {
return;
}

if (!LastDiag) {
assert(false && "Adding a note without main diagnostic");
IgnoreDiagnostics::log(DiagLevel, Info);
return;
}

// If a diagnostic was suppressed due to the suppression filter,
// also suppress notes associated with it.
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
return;

if (!Info.getFixItHints().empty()) {
// A clang note with fix-it is not a separate diagnostic in clangd. We
// attach it as a Fix to the main diagnostic instead.
Expand All @@ -788,7 +786,7 @@ void StoreDiags::flushLastDiag() {
LastDiag.reset();
});

if (isExcluded(*LastDiag))
if (LastDiag->Severity == DiagnosticsEngine::Ignored)
return;
// Move errors that occur from headers into main file.
if (!LastDiag->InsideMainFile && LastDiagLoc && LastDiagOriginallyError) {
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clangd/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ class StoreDiags : public DiagnosticConsumer {
SourceManager *OrigSrcMgr = nullptr;

llvm::DenseSet<std::pair<unsigned, unsigned>> IncludedErrorLocations;
bool LastPrimaryDiagnosticWasSuppressed = false;
};

/// Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
Expand Down
6 changes: 5 additions & 1 deletion clang-tools-extra/clangd/tool/ClangdMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,11 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var

if (CheckFile.getNumOccurrences()) {
llvm::SmallString<256> Path;
llvm::sys::fs::real_path(CheckFile, Path, /*expand_tilde=*/true);
if (auto Error =
llvm::sys::fs::real_path(CheckFile, Path, /*expand_tilde=*/true)) {
elog("Failed to resolve path {0}: {1}", CheckFile, Error.message());
return 1;
}
log("Entering check mode (no LSP server)");
uint32_t Begin = 0, End = std::numeric_limits<uint32_t>::max();
if (!CheckFileLines.empty()) {
Expand Down
12 changes: 6 additions & 6 deletions clang-tools-extra/clangd/unittests/CompileCommandsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ TEST(CommandMangler, Everything) {
Mangler.ClangPath = testPath("fake/clang");
Mangler.ResourceDir = testPath("fake/resources");
Mangler.Sysroot = testPath("fake/sysroot");
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load", "-Xclang",
"plugin", "-MF", "dep", "foo.cc"};
std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load",
"-Xclang", "plugin", "-MF",
"dep", "--", "foo.cc"};
Mangler.adjust(Cmd);
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc",
"-fsyntax-only",
EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "-fsyntax-only",
"-resource-dir=" + testPath("fake/resources"),
"-isysroot", testPath("fake/sysroot")));
"-isysroot", testPath("fake/sysroot"), "--",
"foo.cc"));
}

TEST(CommandMangler, ResourceDir) {
Expand Down Expand Up @@ -378,4 +379,3 @@ TEST(PrintArgvTest, All) {
} // namespace
} // namespace clangd
} // namespace clang

4 changes: 2 additions & 2 deletions clang-tools-extra/clangd/unittests/ConfigCompileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ TEST_F(ConfigCompileTests, Condition) {
TEST_F(ConfigCompileTests, CompileCommands) {
Frag.CompileFlags.Add.emplace_back("-foo");
Frag.CompileFlags.Remove.emplace_back("--include-directory=");
std::vector<std::string> Argv = {"clang", "-I", "bar/", "a.cc"};
std::vector<std::string> Argv = {"clang", "-I", "bar/", "--", "a.cc"};
EXPECT_TRUE(compileAndApply());
EXPECT_THAT(Conf.CompileFlags.Edits, SizeIs(2));
for (auto &Edit : Conf.CompileFlags.Edits)
Edit(Argv);
EXPECT_THAT(Argv, ElementsAre("clang", "a.cc", "-foo"));
EXPECT_THAT(Argv, ElementsAre("clang", "-foo", "--", "a.cc"));
}

TEST_F(ConfigCompileTests, CompilationDatabase) {
Expand Down
32 changes: 32 additions & 0 deletions clang-tools-extra/clangd/unittests/FeatureModulesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "Annotations.h"
#include "FeatureModule.h"
#include "Selection.h"
#include "TestTU.h"
Expand Down Expand Up @@ -53,6 +54,37 @@ TEST(FeatureModulesTest, ContributesTweak) {
EXPECT_EQ(Actual->get()->id(), TweakID);
}

TEST(FeatureModulesTest, SuppressDiags) {
struct DiagModifierModule final : public FeatureModule {
struct Listener : public FeatureModule::ASTListener {
void sawDiagnostic(const clang::Diagnostic &Info,
clangd::Diag &Diag) override {
Diag.Severity = DiagnosticsEngine::Ignored;
}
};
std::unique_ptr<ASTListener> astListeners() override {
return std::make_unique<Listener>();
};
};
FeatureModuleSet FMS;
FMS.add(std::make_unique<DiagModifierModule>());

Annotations Code("[[test]]; /* error-ok */");
TestTU TU;
TU.Code = Code.code().str();

{
auto AST = TU.build();
EXPECT_THAT(*AST.getDiagnostics(), testing::Not(testing::IsEmpty()));
}

TU.FeatureModules = &FMS;
{
auto AST = TU.build();
EXPECT_THAT(*AST.getDiagnostics(), testing::IsEmpty());
}
}

} // namespace
} // namespace clangd
} // namespace clang
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t

template <typename T>
struct Iterator {
void operator++();
const T &operator*() const;
bool operator!=(const Iterator &) const;
typedef const T &const_reference;
};

struct ExpensiveToCopyType {
ExpensiveToCopyType();
virtual ~ExpensiveToCopyType();
const ExpensiveToCopyType &reference() const;
const ExpensiveToCopyType *pointer() const;
Iterator<ExpensiveToCopyType> begin() const;
Iterator<ExpensiveToCopyType> end() const;
void nonConstMethod();
bool constMethod() const;
};
Expand Down Expand Up @@ -589,3 +599,41 @@ void positiveUnusedReferenceIsRemoved() {
// CHECK-FIXES-NOT: auto TrailingCommentRemoved = ExpensiveTypeReference(); // Trailing comment.
// clang-format on
}

void negativeloopedOverObjectIsModified() {
ExpensiveToCopyType Orig;
for (const auto &Element : Orig) {
const auto Copy = Element;
Orig.nonConstMethod();
Copy.constMethod();
}

auto Lambda = []() {
ExpensiveToCopyType Orig;
for (const auto &Element : Orig) {
const auto Copy = Element;
Orig.nonConstMethod();
Copy.constMethod();
}
};
}

void negativeReferenceIsInitializedOutsideOfBlock() {
ExpensiveToCopyType Orig;
const auto &E2 = Orig;
if (1 != 2 * 3) {
const auto C2 = E2;
Orig.nonConstMethod();
C2.constMethod();
}

auto Lambda = []() {
ExpensiveToCopyType Orig;
const auto &E2 = Orig;
if (1 != 2 * 3) {
const auto C2 = E2;
Orig.nonConstMethod();
C2.constMethod();
}
};
}
Loading

0 comments on commit dbde9c1

Please sign in to comment.