Skip to content

Commit 2c62077

Browse files
committed
Merge from 'master' to 'sycl-web' (#2)
CONFLICT (add/add): Merge conflict in clang/lib/Sema/SemaSYCL.cpp CONFLICT (content): Merge conflict in clang/lib/Sema/SemaDecl.cpp CONFLICT (content): Merge conflict in clang/include/clang/Sema/Sema.h
2 parents 568f391 + cf6cc66 commit 2c62077

File tree

592 files changed

+16559
-5255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

592 files changed

+16559
-5255
lines changed

clang-tools-extra/clang-tidy/abseil/AbseilTidyModule.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#include "NoInternalDependenciesCheck.h"
2222
#include "NoNamespaceCheck.h"
2323
#include "RedundantStrcatCallsCheck.h"
24-
#include "StringFindStartswithCheck.h"
2524
#include "StrCatAppendCheck.h"
25+
#include "StringFindStartswithCheck.h"
26+
#include "StringFindStrContainsCheck.h"
2627
#include "TimeComparisonCheck.h"
2728
#include "TimeSubtractionCheck.h"
2829
#include "UpgradeDurationConversionsCheck.h"
@@ -61,6 +62,8 @@ class AbseilModule : public ClangTidyModule {
6162
"abseil-str-cat-append");
6263
CheckFactories.registerCheck<StringFindStartswithCheck>(
6364
"abseil-string-find-startswith");
65+
CheckFactories.registerCheck<StringFindStrContainsCheck>(
66+
"abseil-string-find-str-contains");
6467
CheckFactories.registerCheck<TimeComparisonCheck>(
6568
"abseil-time-comparison");
6669
CheckFactories.registerCheck<TimeSubtractionCheck>(

clang-tools-extra/clang-tidy/abseil/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_clang_library(clangTidyAbseilModule
2020
RedundantStrcatCallsCheck.cpp
2121
StrCatAppendCheck.cpp
2222
StringFindStartswithCheck.cpp
23+
StringFindStrContainsCheck.cpp
2324
TimeComparisonCheck.cpp
2425
TimeSubtractionCheck.cpp
2526
UpgradeDurationConversionsCheck.cpp
@@ -32,4 +33,5 @@ add_clang_library(clangTidyAbseilModule
3233
clangTidy
3334
clangTidyUtils
3435
clangTooling
36+
clangTransformer
3537
)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//===--- StringFindStrContainsCheck.cc - clang-tidy------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "StringFindStrContainsCheck.h"
10+
11+
#include "../utils/OptionsUtils.h"
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/ASTMatchers/ASTMatchers.h"
14+
#include "clang/Frontend/CompilerInstance.h"
15+
#include "clang/Tooling/Transformer/RewriteRule.h"
16+
#include "clang/Tooling/Transformer/Stencil.h"
17+
18+
using namespace clang::ast_matchers;
19+
20+
namespace clang {
21+
namespace tidy {
22+
namespace abseil {
23+
24+
using ::clang::transformer::applyFirst;
25+
using ::clang::transformer::cat;
26+
using ::clang::transformer::change;
27+
using ::clang::transformer::makeRule;
28+
using ::clang::transformer::node;
29+
30+
static const char DefaultStringLikeClasses[] = "::std::basic_string;"
31+
"::std::basic_string_view;"
32+
"::absl::string_view";
33+
static const char DefaultAbseilStringsMatchHeader[] = "absl/strings/match.h";
34+
35+
static llvm::Optional<transformer::RewriteRule>
36+
MakeRule(const LangOptions &LangOpts,
37+
const ClangTidyCheck::OptionsView &Options) {
38+
// Parse options.
39+
//
40+
// FIXME(tdl-g): These options are being parsed redundantly with the
41+
// constructor because TransformerClangTidyCheck forces us to provide MakeRule
42+
// before "this" is fully constructed, but StoreOptions requires us to store
43+
// the parsed options in "this". We need to fix TransformerClangTidyCheck and
44+
// then we can clean this up.
45+
const std::vector<std::string> StringLikeClassNames =
46+
utils::options::parseStringList(
47+
Options.get("StringLikeClasses", DefaultStringLikeClasses));
48+
const std::string AbseilStringsMatchHeader =
49+
Options.get("AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader);
50+
51+
auto StringLikeClass = cxxRecordDecl(hasAnyName(SmallVector<StringRef, 4>(
52+
StringLikeClassNames.begin(), StringLikeClassNames.end())));
53+
auto StringType =
54+
hasUnqualifiedDesugaredType(recordType(hasDeclaration(StringLikeClass)));
55+
auto CharStarType =
56+
hasUnqualifiedDesugaredType(pointerType(pointee(isAnyCharacter())));
57+
auto StringNpos = declRefExpr(
58+
to(varDecl(hasName("npos"), hasDeclContext(StringLikeClass))));
59+
auto StringFind = cxxMemberCallExpr(
60+
callee(cxxMethodDecl(
61+
hasName("find"),
62+
hasParameter(0, parmVarDecl(anyOf(hasType(StringType),
63+
hasType(CharStarType)))))),
64+
on(hasType(StringType)), hasArgument(0, expr().bind("parameter_to_find")),
65+
anyOf(hasArgument(1, integerLiteral(equals(0))),
66+
hasArgument(1, cxxDefaultArgExpr())),
67+
onImplicitObjectArgument(expr().bind("string_being_searched")));
68+
69+
tooling::RewriteRule rule = applyFirst(
70+
{makeRule(binaryOperator(hasOperatorName("=="),
71+
hasOperands(ignoringParenImpCasts(StringNpos),
72+
ignoringParenImpCasts(StringFind))),
73+
change(cat("!absl::StrContains(", node("string_being_searched"),
74+
", ", node("parameter_to_find"), ")")),
75+
cat("use !absl::StrContains instead of find() == npos")),
76+
makeRule(binaryOperator(hasOperatorName("!="),
77+
hasOperands(ignoringParenImpCasts(StringNpos),
78+
ignoringParenImpCasts(StringFind))),
79+
change(cat("absl::StrContains(", node("string_being_searched"),
80+
", ", node("parameter_to_find"), ")")),
81+
cat("use absl::StrContains instead of find() != npos"))});
82+
addInclude(rule, AbseilStringsMatchHeader);
83+
return rule;
84+
}
85+
86+
StringFindStrContainsCheck::StringFindStrContainsCheck(
87+
StringRef Name, ClangTidyContext *Context)
88+
: TransformerClangTidyCheck(&MakeRule, Name, Context),
89+
StringLikeClassesOption(utils::options::parseStringList(
90+
Options.get("StringLikeClasses", DefaultStringLikeClasses))),
91+
AbseilStringsMatchHeaderOption(Options.get(
92+
"AbseilStringsMatchHeader", DefaultAbseilStringsMatchHeader)) {}
93+
94+
bool StringFindStrContainsCheck::isLanguageVersionSupported(
95+
const LangOptions &LangOpts) const {
96+
return LangOpts.CPlusPlus11;
97+
}
98+
99+
void StringFindStrContainsCheck::storeOptions(
100+
ClangTidyOptions::OptionMap &Opts) {
101+
TransformerClangTidyCheck::storeOptions(Opts);
102+
Options.store(Opts, "StringLikeClasses",
103+
utils::options::serializeStringList(StringLikeClassesOption));
104+
Options.store(Opts, "AbseilStringsMatchHeader",
105+
AbseilStringsMatchHeaderOption);
106+
}
107+
108+
} // namespace abseil
109+
} // namespace tidy
110+
} // namespace clang
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===--- StringFindStrContainsCheck.h - clang-tidy---------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_STRINGFINDSTRCONTAINSCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_STRINGFINDSTRCONTAINSCHECK_H
11+
12+
#include "../ClangTidy.h"
13+
#include "../utils/TransformerClangTidyCheck.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace abseil {
18+
19+
/// Finds s.find(...) == string::npos comparisons (for various string-like
20+
/// types) and suggests replacing with absl::StrContains.
21+
///
22+
/// For the user-facing documentation see:
23+
/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-string-find-str-contains.html
24+
class StringFindStrContainsCheck : public utils::TransformerClangTidyCheck {
25+
public:
26+
StringFindStrContainsCheck(StringRef Name, ClangTidyContext *Context);
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
28+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
29+
30+
private:
31+
const std::vector<std::string> StringLikeClassesOption;
32+
const std::string AbseilStringsMatchHeaderOption;
33+
};
34+
35+
} // namespace abseil
36+
} // namespace tidy
37+
} // namespace clang
38+
39+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_STRINGFINDSTRCONTAINSCHECK_H

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ ClangdServer::Options ClangdServer::optsForTest() {
114114
Opts.StorePreamblesInMemory = true;
115115
Opts.AsyncThreadsCount = 4; // Consistent!
116116
Opts.TheiaSemanticHighlighting = true;
117+
Opts.AsyncPreambleBuilds = true;
117118
return Opts;
118119
}
119120

@@ -123,6 +124,7 @@ ClangdServer::Options::operator TUScheduler::Options() const {
123124
Opts.RetentionPolicy = RetentionPolicy;
124125
Opts.StorePreamblesInMemory = StorePreamblesInMemory;
125126
Opts.UpdateDebounce = UpdateDebounce;
127+
Opts.AsyncPreambleBuilds = AsyncPreambleBuilds;
126128
return Opts;
127129
}
128130

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class ClangdServer {
9797

9898
/// Cached preambles are potentially large. If false, store them on disk.
9999
bool StorePreamblesInMemory = true;
100+
/// Reuse even stale preambles, and rebuild them in the background.
101+
/// This improves latency at the cost of accuracy.
102+
bool AsyncPreambleBuilds = false;
100103

101104
/// If true, ClangdServer builds a dynamic in-memory index for symbols in
102105
/// opened files and uses the index to augment code completion results.

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ struct SemaCompleteInput {
10311031
PathRef FileName;
10321032
const tooling::CompileCommand &Command;
10331033
const PreambleData &Preamble;
1034-
const PreamblePatch &Patch;
1034+
llvm::Optional<const PreamblePatch> Patch;
10351035
llvm::StringRef Contents;
10361036
size_t Offset;
10371037
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS;
@@ -1105,7 +1105,8 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
11051105
PreambleBounds PreambleRegion =
11061106
ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0);
11071107
bool CompletingInPreamble = PreambleRegion.Size > Input.Offset;
1108-
Input.Patch.apply(*CI);
1108+
if (Input.Patch)
1109+
Input.Patch->apply(*CI);
11091110
// NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise
11101111
// the remapped buffers do not get freed.
11111112
auto Clang = prepareCompilerInstance(
@@ -1767,7 +1768,8 @@ codeComplete(PathRef FileName, const tooling::CompileCommand &Command,
17671768
: std::move(Flow).run({FileName, Command, *Preamble,
17681769
// We want to serve code completions with
17691770
// low latency, so don't bother patching.
1770-
PreamblePatch(), Contents, *Offset, VFS});
1771+
/*PreamblePatch=*/llvm::None, Contents,
1772+
*Offset, VFS});
17711773
}
17721774

17731775
SignatureHelp signatureHelp(PathRef FileName,
@@ -1792,10 +1794,11 @@ SignatureHelp signatureHelp(PathRef FileName,
17921794
PI.CompileCommand = Command;
17931795
PI.Contents = Contents.str();
17941796
PI.FS = std::move(VFS);
1795-
auto PP = PreamblePatch::create(FileName, PI, Preamble);
17961797
semaCodeComplete(
17971798
std::make_unique<SignatureHelpCollector>(Options, Index, Result), Options,
1798-
{FileName, Command, Preamble, PP, Contents, *Offset, std::move(PI.FS)});
1799+
{FileName, Command, Preamble,
1800+
PreamblePatch::create(FileName, PI, Preamble), Contents, *Offset,
1801+
std::move(PI.FS)});
17991802
return Result;
18001803
}
18011804

clang-tools-extra/clangd/Headers.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Headers.h"
1010
#include "Compiler.h"
11+
#include "Preamble.h"
1112
#include "SourceCode.h"
1213
#include "support/Logger.h"
1314
#include "clang/Basic/SourceLocation.h"
@@ -23,11 +24,6 @@ namespace clang {
2324
namespace clangd {
2425
namespace {
2526

26-
bool isMainFile(llvm::StringRef FileName, const SourceManager &SM) {
27-
auto FE = SM.getFileManager().getFile(FileName);
28-
return FE && *FE == SM.getFileEntryForID(SM.getMainFileID());
29-
}
30-
3127
class RecordHeaders : public PPCallbacks {
3228
public:
3329
RecordHeaders(const SourceManager &SM, IncludeStructure *Out)
@@ -44,17 +40,8 @@ class RecordHeaders : public PPCallbacks {
4440
SrcMgr::CharacteristicKind FileKind) override {
4541
auto MainFID = SM.getMainFileID();
4642
// If an include is part of the preamble patch, translate #line directives.
47-
if (InBuiltinFile) {
48-
auto Presumed = SM.getPresumedLoc(HashLoc);
49-
// Presumed locations will have an invalid file id when #line directive
50-
// changes the filename.
51-
if (Presumed.getFileID().isInvalid() &&
52-
isMainFile(Presumed.getFilename(), SM)) {
53-
// Now we'll hit the case below.
54-
HashLoc = SM.translateLineCol(MainFID, Presumed.getLine(),
55-
Presumed.getColumn());
56-
}
57-
}
43+
if (InBuiltinFile)
44+
HashLoc = translatePreamblePatchLocation(HashLoc, SM);
5845

5946
// Record main-file inclusions (including those mapped from the preamble
6047
// patch).

clang-tools-extra/clangd/Hover.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ llvm::Optional<StringRef> fieldName(const Expr *E) {
376376
const auto *ME = llvm::dyn_cast<MemberExpr>(E->IgnoreCasts());
377377
if (!ME || !llvm::isa<CXXThisExpr>(ME->getBase()->IgnoreCasts()))
378378
return llvm::None;
379-
const auto *Field =
380-
llvm::dyn_cast<FieldDecl>(ME->getMemberDecl());
379+
const auto *Field = llvm::dyn_cast<FieldDecl>(ME->getMemberDecl());
381380
if (!Field || !Field->getDeclName().isIdentifier())
382381
return llvm::None;
383382
return Field->getDeclName().getAsIdentifierInfo()->getName();
@@ -556,7 +555,14 @@ HoverInfo getHoverContents(const DefinedMacro &Macro, ParsedAST &AST) {
556555
// Try to get the full definition, not just the name
557556
SourceLocation StartLoc = Macro.Info->getDefinitionLoc();
558557
SourceLocation EndLoc = Macro.Info->getDefinitionEndLoc();
559-
if (EndLoc.isValid()) {
558+
// Ensure that EndLoc is a valid offset. For example it might come from
559+
// preamble, and source file might've changed, in such a scenario EndLoc still
560+
// stays valid, but getLocForEndOfToken will fail as it is no longer a valid
561+
// offset.
562+
// Note that this check is just to ensure there's text data inside the range.
563+
// It will still succeed even when the data inside the range is irrelevant to
564+
// macro definition.
565+
if (SM.getPresumedLoc(EndLoc, /*UseLineDirectives=*/false).isValid()) {
560566
EndLoc = Lexer::getLocForEndOfToken(EndLoc, 0, SM, AST.getLangOpts());
561567
bool Invalid;
562568
StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc), &Invalid);
@@ -873,20 +879,20 @@ llvm::Optional<llvm::StringRef> getBacktickQuoteRange(llvm::StringRef Line,
873879
if (!Suffix.empty() && !AfterEndChars.contains(Suffix.front()))
874880
return llvm::None;
875881

876-
return Line.slice(Offset, Next+1);
882+
return Line.slice(Offset, Next + 1);
877883
}
878884

879885
void parseDocumentationLine(llvm::StringRef Line, markup::Paragraph &Out) {
880886
// Probably this is appendText(Line), but scan for something interesting.
881887
for (unsigned I = 0; I < Line.size(); ++I) {
882888
switch (Line[I]) {
883-
case '`':
884-
if (auto Range = getBacktickQuoteRange(Line, I)) {
885-
Out.appendText(Line.substr(0, I));
886-
Out.appendCode(Range->trim("`"), /*Preserve=*/true);
887-
return parseDocumentationLine(Line.substr(I+Range->size()), Out);
888-
}
889-
break;
889+
case '`':
890+
if (auto Range = getBacktickQuoteRange(Line, I)) {
891+
Out.appendText(Line.substr(0, I));
892+
Out.appendCode(Range->trim("`"), /*Preserve=*/true);
893+
return parseDocumentationLine(Line.substr(I + Range->size()), Out);
894+
}
895+
break;
890896
}
891897
}
892898
Out.appendText(Line).appendSpace();

0 commit comments

Comments
 (0)