Skip to content

Commit 2a2ea6b

Browse files
committed
Merge remote-tracking branch 'upstream/release/15.x' into rustc/15.0-2022-08-09
2 parents 4b85255 + dccd061 commit 2a2ea6b

File tree

73 files changed

+1699
-540
lines changed

Some content is hidden

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

73 files changed

+1699
-540
lines changed

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> {
472472
checkSingleStatement(If->getThen(), parseReturnLiteralBool);
473473
if (ThenReturnBool &&
474474
ThenReturnBool.Bool != TrailingReturnBool.Bool) {
475-
if (Check->ChainedConditionalReturn ||
476-
(!PrevIf && If->getElse() == nullptr)) {
475+
if ((Check->ChainedConditionalReturn || !PrevIf) &&
476+
If->getElse() == nullptr) {
477477
Check->replaceCompoundReturnWithCondition(
478478
Context, cast<ReturnStmt>(*Second), TrailingReturnBool.Bool,
479479
If, ThenReturnBool.Item);

clang-tools-extra/clangd/Headers.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
namespace clang {
2323
namespace clangd {
2424

25-
const char IWYUPragmaKeep[] = "// IWYU pragma: keep";
26-
const char IWYUPragmaExport[] = "// IWYU pragma: export";
27-
const char IWYUPragmaBeginExports[] = "// IWYU pragma: begin_exports";
25+
llvm::Optional<StringRef> parseIWYUPragma(const char *Text) {
26+
// This gets called for every comment seen in the preamble, so it's quite hot.
27+
constexpr llvm::StringLiteral IWYUPragma = "// IWYU pragma: ";
28+
if (strncmp(Text, IWYUPragma.data(), IWYUPragma.size()))
29+
return llvm::None;
30+
Text += IWYUPragma.size();
31+
const char *End = Text;
32+
while (*End != 0 && *End != '\n')
33+
++End;
34+
return StringRef(Text, End - Text);
35+
}
2836

2937
class IncludeStructure::RecordHeaders : public PPCallbacks,
3038
public CommentHandler {
@@ -129,10 +137,10 @@ class IncludeStructure::RecordHeaders : public PPCallbacks,
129137
}
130138

131139
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
132-
bool Err = false;
133-
llvm::StringRef Text = SM.getCharacterData(Range.getBegin(), &Err);
134-
if (Err)
140+
auto Pragma = parseIWYUPragma(SM.getCharacterData(Range.getBegin()));
141+
if (!Pragma)
135142
return false;
143+
136144
if (inMainFile()) {
137145
// Given:
138146
//
@@ -150,8 +158,7 @@ class IncludeStructure::RecordHeaders : public PPCallbacks,
150158
// will know that the next inclusion is behind the IWYU pragma.
151159
// FIXME: Support "IWYU pragma: begin_exports" and "IWYU pragma:
152160
// end_exports".
153-
if (!Text.startswith(IWYUPragmaExport) &&
154-
!Text.startswith(IWYUPragmaKeep))
161+
if (!Pragma->startswith("export") && !Pragma->startswith("keep"))
155162
return false;
156163
unsigned Offset = SM.getFileOffset(Range.getBegin());
157164
LastPragmaKeepInMainFileLine =
@@ -161,8 +168,7 @@ class IncludeStructure::RecordHeaders : public PPCallbacks,
161168
// does not support them properly yet, so they will be not marked as
162169
// unused.
163170
// FIXME: Once IncludeCleaner supports export pragmas, remove this.
164-
if (!Text.startswith(IWYUPragmaExport) &&
165-
!Text.startswith(IWYUPragmaBeginExports))
171+
if (!Pragma->startswith("export") && !Pragma->startswith("begin_exports"))
166172
return false;
167173
Out->HasIWYUExport.insert(
168174
*Out->getID(SM.getFileEntryForID(SM.getFileID(Range.getBegin()))));

clang-tools-extra/clangd/Headers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ namespace clangd {
3535
/// Returns true if \p Include is literal include like "path" or <path>.
3636
bool isLiteralInclude(llvm::StringRef Include);
3737

38+
/// If Text begins an Include-What-You-Use directive, returns it.
39+
/// Given "// IWYU pragma: keep", returns "keep".
40+
/// Input is a null-terminated char* as provided by SM.getCharacterData().
41+
/// (This should not be StringRef as we do *not* want to scan for its length).
42+
llvm::Optional<StringRef> parseIWYUPragma(const char *Text);
43+
3844
/// Represents a header file to be #include'd.
3945
struct HeaderFile {
4046
std::string File;

clang-tools-extra/clangd/InlayHints.cpp

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Config.h"
1111
#include "HeuristicResolver.h"
1212
#include "ParsedAST.h"
13+
#include "SourceCode.h"
1314
#include "clang/AST/Decl.h"
1415
#include "clang/AST/DeclarationName.h"
1516
#include "clang/AST/ExprCXX.h"
@@ -192,8 +193,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
192193
public:
193194
InlayHintVisitor(std::vector<InlayHint> &Results, ParsedAST &AST,
194195
const Config &Cfg, llvm::Optional<Range> RestrictRange)
195-
: Results(Results), AST(AST.getASTContext()), Cfg(Cfg),
196-
RestrictRange(std::move(RestrictRange)),
196+
: Results(Results), AST(AST.getASTContext()), Tokens(AST.getTokens()),
197+
Cfg(Cfg), RestrictRange(std::move(RestrictRange)),
197198
MainFileID(AST.getSourceManager().getMainFileID()),
198199
Resolver(AST.getHeuristicResolver()),
199200
TypeHintPolicy(this->AST.getPrintingPolicy()),
@@ -227,8 +228,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
227228
return true;
228229
}
229230

230-
processCall(E->getParenOrBraceRange().getBegin(), E->getConstructor(),
231-
{E->getArgs(), E->getNumArgs()});
231+
processCall(E->getConstructor(), {E->getArgs(), E->getNumArgs()});
232232
return true;
233233
}
234234

@@ -254,7 +254,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
254254
if (!Callee)
255255
return true;
256256

257-
processCall(E->getRParenLoc(), Callee, {E->getArgs(), E->getNumArgs()});
257+
processCall(Callee, {E->getArgs(), E->getNumArgs()});
258258
return true;
259259
}
260260

@@ -278,11 +278,11 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
278278
return true;
279279
}
280280

281-
void addReturnTypeHint(FunctionDecl *D, SourceLocation Loc) {
281+
void addReturnTypeHint(FunctionDecl *D, SourceRange Range) {
282282
auto *AT = D->getReturnType()->getContainedAutoType();
283283
if (!AT || AT->getDeducedType().isNull())
284284
return;
285-
addTypeHint(Loc, D->getReturnType(), /*Prefix=*/"-> ");
285+
addTypeHint(Range, D->getReturnType(), /*Prefix=*/"-> ");
286286
}
287287

288288
bool VisitVarDecl(VarDecl *D) {
@@ -375,21 +375,11 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
375375
private:
376376
using NameVec = SmallVector<StringRef, 8>;
377377

378-
// The purpose of Anchor is to deal with macros. It should be the call's
379-
// opening or closing parenthesis or brace. (Always using the opening would
380-
// make more sense but CallExpr only exposes the closing.) We heuristically
381-
// assume that if this location does not come from a macro definition, then
382-
// the entire argument list likely appears in the main file and can be hinted.
383-
void processCall(SourceLocation Anchor, const FunctionDecl *Callee,
378+
void processCall(const FunctionDecl *Callee,
384379
llvm::ArrayRef<const Expr *> Args) {
385380
if (!Cfg.InlayHints.Parameters || Args.size() == 0 || !Callee)
386381
return;
387382

388-
// If the anchor location comes from a macro defintion, there's nowhere to
389-
// put hints.
390-
if (!AST.getSourceManager().getTopMacroCallerLoc(Anchor).isFileID())
391-
return;
392-
393383
// The parameter name of a move or copy constructor is not very interesting.
394384
if (auto *Ctor = dyn_cast<CXXConstructorDecl>(Callee))
395385
if (Ctor->isCopyOrMoveConstructor())
@@ -637,25 +627,33 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
637627
#undef CHECK_KIND
638628
}
639629

640-
auto FileRange =
641-
toHalfOpenFileRange(AST.getSourceManager(), AST.getLangOpts(), R);
642-
if (!FileRange)
630+
auto LSPRange = getHintRange(R);
631+
if (!LSPRange)
643632
return;
644-
Range LSPRange{
645-
sourceLocToPosition(AST.getSourceManager(), FileRange->getBegin()),
646-
sourceLocToPosition(AST.getSourceManager(), FileRange->getEnd())};
647-
Position LSPPos = Side == HintSide::Left ? LSPRange.start : LSPRange.end;
633+
Position LSPPos = Side == HintSide::Left ? LSPRange->start : LSPRange->end;
648634
if (RestrictRange &&
649635
(LSPPos < RestrictRange->start || !(LSPPos < RestrictRange->end)))
650636
return;
651-
// The hint may be in a file other than the main file (for example, a header
652-
// file that was included after the preamble), do not show in that case.
653-
if (!AST.getSourceManager().isWrittenInMainFile(FileRange->getBegin()))
654-
return;
655637
bool PadLeft = Prefix.consume_front(" ");
656638
bool PadRight = Suffix.consume_back(" ");
657639
Results.push_back(InlayHint{LSPPos, (Prefix + Label + Suffix).str(), Kind,
658-
PadLeft, PadRight, LSPRange});
640+
PadLeft, PadRight, *LSPRange});
641+
}
642+
643+
// Get the range of the main file that *exactly* corresponds to R.
644+
llvm::Optional<Range> getHintRange(SourceRange R) {
645+
const auto &SM = AST.getSourceManager();
646+
auto Spelled = Tokens.spelledForExpanded(Tokens.expandedTokens(R));
647+
// TokenBuffer will return null if e.g. R corresponds to only part of a
648+
// macro expansion.
649+
if (!Spelled || Spelled->empty())
650+
return llvm::None;
651+
// Hint must be within the main file, not e.g. a non-preamble include.
652+
if (SM.getFileID(Spelled->front().location()) != SM.getMainFileID() ||
653+
SM.getFileID(Spelled->back().location()) != SM.getMainFileID())
654+
return llvm::None;
655+
return Range{sourceLocToPosition(SM, Spelled->front().location()),
656+
sourceLocToPosition(SM, Spelled->back().endLocation())};
659657
}
660658

661659
void addTypeHint(SourceRange R, QualType T, llvm::StringRef Prefix) {
@@ -680,6 +678,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
680678

681679
std::vector<InlayHint> &Results;
682680
ASTContext &AST;
681+
const syntax::TokenBuffer &Tokens;
683682
const Config &Cfg;
684683
llvm::Optional<Range> RestrictRange;
685684
FileID MainFileID;

clang-tools-extra/clangd/TidyProvider.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,14 @@ TidyProvider disableUnusableChecks(llvm::ArrayRef<std::string> ExtraBadChecks) {
212212
// code, which is often the case when clangd
213213
// tries to build an AST.
214214
"-bugprone-use-after-move",
215-
// Alias for bugprone-use-after-moe.
216-
"-hicpp-invalid-access-moved");
215+
// Alias for bugprone-use-after-move.
216+
"-hicpp-invalid-access-moved",
217+
218+
// ----- Performance problems -----
219+
220+
// This check runs expensive analysis for each variable.
221+
// It has been observed to increase reparse time by 10x.
222+
"-misc-const-correctness");
217223

218224
size_t Size = BadChecks.size();
219225
for (const std::string &Str : ExtraBadChecks) {

clang-tools-extra/clangd/index/CanonicalIncludes.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
namespace clang {
1818
namespace clangd {
1919
namespace {
20-
const char IWYUPragma[] = "// IWYU pragma: private, include ";
21-
2220
const std::pair<llvm::StringRef, llvm::StringRef> IncludeMappings[] = {
2321
{"include/__stddef_max_align_t.h", "<cstddef>"},
2422
{"include/__wmmintrin_aes.h", "<wmmintrin.h>"},
@@ -712,17 +710,17 @@ collectIWYUHeaderMaps(CanonicalIncludes *Includes) {
712710
PragmaCommentHandler(CanonicalIncludes *Includes) : Includes(Includes) {}
713711

714712
bool HandleComment(Preprocessor &PP, SourceRange Range) override {
715-
llvm::StringRef Text =
716-
Lexer::getSourceText(CharSourceRange::getCharRange(Range),
717-
PP.getSourceManager(), PP.getLangOpts());
718-
if (!Text.consume_front(IWYUPragma))
713+
auto Pragma = parseIWYUPragma(
714+
PP.getSourceManager().getCharacterData(Range.getBegin()));
715+
if (!Pragma || !Pragma->consume_front("private, include "))
719716
return false;
720717
auto &SM = PP.getSourceManager();
721718
// We always insert using the spelling from the pragma.
722719
if (auto *FE = SM.getFileEntryForID(SM.getFileID(Range.getBegin())))
723-
Includes->addMapping(
724-
FE->getLastRef(),
725-
isLiteralInclude(Text) ? Text.str() : ("\"" + Text + "\"").str());
720+
Includes->addMapping(FE->getLastRef(),
721+
isLiteralInclude(*Pragma)
722+
? Pragma->str()
723+
: ("\"" + *Pragma + "\"").str());
726724
return false;
727725
}
728726

clang-tools-extra/clangd/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ config.python_executable = "@Python3_EXECUTABLE@"
1010
config.clang_tools_dir = lit_config.substitute("@CURRENT_TOOLS_DIR@")
1111
config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
1212
config.llvm_libs_dir = lit_config.substitute("@LLVM_LIBS_DIR@")
13+
config.llvm_shlib_dir = "@SHLIBDIR@"
1314

1415
config.clangd_source_dir = "@CMAKE_CURRENT_SOURCE_DIR@/.."
1516
config.clangd_binary_dir = "@CMAKE_CURRENT_BINARY_DIR@/.."

clang-tools-extra/clangd/unittests/HeadersTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "Headers.h"
1010

1111
#include "Compiler.h"
12+
#include "Matchers.h"
1213
#include "TestFS.h"
1314
#include "TestTU.h"
1415
#include "clang/Basic/TokenKinds.h"
@@ -30,6 +31,7 @@ namespace {
3031
using ::testing::AllOf;
3132
using ::testing::Contains;
3233
using ::testing::ElementsAre;
34+
using ::testing::Eq;
3335
using ::testing::IsEmpty;
3436
using ::testing::Not;
3537
using ::testing::UnorderedElementsAre;
@@ -445,6 +447,18 @@ TEST_F(HeadersTest, HasIWYUPragmas) {
445447
EXPECT_FALSE(Includes.hasIWYUExport(getID("none.h", Includes)));
446448
}
447449

450+
TEST(Headers, ParseIWYUPragma) {
451+
EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep"), HasValue(Eq("keep")));
452+
EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep\netc"),
453+
HasValue(Eq("keep")));
454+
EXPECT_EQ(parseIWYUPragma("/* IWYU pragma: keep"), llvm::None)
455+
<< "Only // comments supported!";
456+
EXPECT_EQ(parseIWYUPragma("// IWYU pragma: keep"), llvm::None)
457+
<< "Sensitive to whitespace";
458+
EXPECT_EQ(parseIWYUPragma("// IWYU pragma:keep"), llvm::None)
459+
<< "Sensitive to whitespace";
460+
}
461+
448462
} // namespace
449463
} // namespace clangd
450464
} // namespace clang

clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,15 @@ TEST(ParameterHints, Macros) {
820820
}
821821
)cpp",
822822
ExpectedHint{"param: ", "param"});
823+
824+
// If the macro expands to multiple arguments, don't hint it.
825+
assertParameterHints(R"cpp(
826+
void foo(double x, double y);
827+
#define CONSTANTS 3.14, 2.72
828+
void bar() {
829+
foo(CONSTANTS);
830+
}
831+
)cpp");
823832
}
824833

825834
TEST(ParameterHints, ConstructorParens) {

clang-tools-extra/test/clang-tidy/checkers/readability/simplify-bool-expr-chained-conditional-return.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ bool complex_chained_if_return_return_negated(int i) {
9292
// CHECK-FIXES: {{^}} }{{$}}
9393
// CHECK-FIXES: {{^ return i <= 10;$}}
9494
// CHECK-FIXES: {{^}$}}
95+
96+
97+
bool PR57819(int x) {
98+
// False positive introduced in clang-tidy-15
99+
// Expect no warning here.
100+
if (x > 0)
101+
return false;
102+
else {
103+
}
104+
return true;
105+
}

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,21 @@ void CodeGenFunction::EmitCaseStmt(const CaseStmt &S,
15091509

15101510
llvm::ConstantInt *CaseVal =
15111511
Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
1512+
1513+
// Emit debuginfo for the case value if it is an enum value.
1514+
const ConstantExpr *CE;
1515+
if (auto ICE = dyn_cast<ImplicitCastExpr>(S.getLHS()))
1516+
CE = dyn_cast<ConstantExpr>(ICE->getSubExpr());
1517+
else
1518+
CE = dyn_cast<ConstantExpr>(S.getLHS());
1519+
if (CE) {
1520+
if (auto DE = dyn_cast<DeclRefExpr>(CE->getSubExpr()))
1521+
if (CGDebugInfo *Dbg = getDebugInfo())
1522+
if (CGM.getCodeGenOpts().hasReducedDebugInfo())
1523+
Dbg->EmitGlobalVariable(DE->getDecl(),
1524+
APValue(llvm::APSInt(CaseVal->getValue())));
1525+
}
1526+
15121527
if (SwitchLikelihood)
15131528
SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));
15141529

clang/lib/Sema/SemaInit.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,10 @@ void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field,
695695
// member of reference type uninitialized, the program is
696696
// ill-formed.
697697
SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
698-
<< Field->getType()
699-
<< ILE->getSyntacticForm()->getSourceRange();
700-
SemaRef.Diag(Field->getLocation(),
701-
diag::note_uninit_reference_member);
698+
<< Field->getType()
699+
<< (ILE->isSyntacticForm() ? ILE : ILE->getSyntacticForm())
700+
->getSourceRange();
701+
SemaRef.Diag(Field->getLocation(), diag::note_uninit_reference_member);
702702
}
703703
hadError = true;
704704
return;

0 commit comments

Comments
 (0)