Skip to content

Commit 2d7a9b5

Browse files
author
Jenkins
committed
merge main into amd-staging
Change-Id: I20a868eaf8a483b19cf946008737f8eb94f849e7
2 parents de4bb85 + 5e6b4be commit 2d7a9b5

File tree

116 files changed

+3436
-615
lines changed

Some content is hidden

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

116 files changed

+3436
-615
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ Bug Fixes in This Version
170170
a member class template for an implicit instantiation of a class template.
171171

172172
- Fixed missing warnings when doing bool-like conversions in C23 (`#79435 <https://github.com/llvm/llvm-project/issues/79435>`_).
173+
- Clang's ``-Wshadow`` no longer warns when an init-capture is named the same as
174+
a class field unless the lambda can capture this.
175+
Fixes (`#71976 <https://github.com/llvm/llvm-project/issues/71976>`_)
173176

174177
- Clang now accepts qualified partial/explicit specializations of variable templates that
175178
are not nominable in the lookup context of the specialization.

clang/include/clang/Sema/ScopeInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,8 @@ class LambdaScopeInfo final :
942942
/// that were defined in parent contexts. Used to avoid warnings when the
943943
/// shadowed variables are uncaptured by this lambda.
944944
struct ShadowedOuterDecl {
945-
const VarDecl *VD;
946-
const VarDecl *ShadowedDecl;
945+
const NamedDecl *VD;
946+
const NamedDecl *ShadowedDecl;
947947
};
948948
llvm::SmallVector<ShadowedOuterDecl, 4> ShadowingDecls;
949949

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/LangOptions.h"
1818
#include "clang/Basic/MacroBuilder.h"
1919
#include "clang/Basic/TargetBuiltins.h"
20+
#include "llvm/ADT/SmallString.h"
2021
using namespace clang;
2122
using namespace clang::targets;
2223

@@ -279,13 +280,25 @@ void AMDGPUTargetInfo::getTargetDefines(const LangOptions &Opts,
279280
if (GPUKind == llvm::AMDGPU::GK_NONE && !IsHIPHost)
280281
return;
281282

282-
StringRef CanonName = isAMDGCN(getTriple()) ? getArchNameAMDGCN(GPUKind)
283-
: getArchNameR600(GPUKind);
283+
llvm::SmallString<16> CanonName =
284+
(isAMDGCN(getTriple()) ? getArchNameAMDGCN(GPUKind)
285+
: getArchNameR600(GPUKind));
286+
287+
// Sanitize the name of generic targets.
288+
// e.g. gfx10.1-generic -> gfx10_1_generic
289+
if (GPUKind >= llvm::AMDGPU::GK_AMDGCN_GENERIC_FIRST &&
290+
GPUKind <= llvm::AMDGPU::GK_AMDGCN_GENERIC_LAST) {
291+
std::replace(CanonName.begin(), CanonName.end(), '.', '_');
292+
std::replace(CanonName.begin(), CanonName.end(), '-', '_');
293+
}
294+
284295
Builder.defineMacro(Twine("__") + Twine(CanonName) + Twine("__"));
285296
// Emit macros for gfx family e.g. gfx906 -> __GFX9__, gfx1030 -> __GFX10___
286297
if (isAMDGCN(getTriple()) && !IsHIPHost) {
287-
assert(CanonName.starts_with("gfx") && "Invalid amdgcn canonical name");
288-
Builder.defineMacro(Twine("__") + Twine(CanonName.drop_back(2).upper()) +
298+
assert(StringRef(CanonName).starts_with("gfx") &&
299+
"Invalid amdgcn canonical name");
300+
StringRef CanonFamilyName = getArchFamilyNameAMDGCN(GPUKind);
301+
Builder.defineMacro(Twine("__") + Twine(CanonFamilyName.upper()) +
289302
Twine("__"));
290303
Builder.defineMacro("__amdgcn_processor__",
291304
Twine("\"") + Twine(CanonName) + Twine("\""));

clang/lib/Format/FormatToken.cpp

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
#include "FormatToken.h"
1616
#include "ContinuationIndenter.h"
17-
#include "llvm/ADT/SmallVector.h"
18-
#include "llvm/Support/Debug.h"
19-
#include <climits>
17+
#include "TokenAnalyzer.h"
2018

2119
namespace clang {
2220
namespace format {
@@ -34,41 +32,9 @@ const char *getTokenTypeName(TokenType Type) {
3432
return nullptr;
3533
}
3634

37-
// FIXME: This is copy&pasted from Sema. Put it in a common place and remove
38-
// duplication.
3935
bool FormatToken::isSimpleTypeSpecifier() const {
40-
switch (Tok.getKind()) {
41-
case tok::kw_short:
42-
case tok::kw_long:
43-
case tok::kw___int64:
44-
case tok::kw___int128:
45-
case tok::kw_signed:
46-
case tok::kw_unsigned:
47-
case tok::kw_void:
48-
case tok::kw_char:
49-
case tok::kw_int:
50-
case tok::kw_half:
51-
case tok::kw_float:
52-
case tok::kw_double:
53-
case tok::kw___bf16:
54-
case tok::kw__Float16:
55-
case tok::kw___float128:
56-
case tok::kw___ibm128:
57-
case tok::kw_wchar_t:
58-
case tok::kw_bool:
59-
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
60-
#include "clang/Basic/TransformTypeTraits.def"
61-
case tok::annot_typename:
62-
case tok::kw_char8_t:
63-
case tok::kw_char16_t:
64-
case tok::kw_char32_t:
65-
case tok::kw_typeof:
66-
case tok::kw_decltype:
67-
case tok::kw__Atomic:
68-
return true;
69-
default:
70-
return false;
71-
}
36+
assert(LangOpts.CPlusPlus);
37+
return Tok.isSimpleTypeSpecifier(LangOpts);
7238
}
7339

7440
bool FormatToken::isTypeOrIdentifier() const {

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "FormatTokenLexer.h"
16-
#include "FormatToken.h"
17-
#include "clang/Basic/SourceLocation.h"
18-
#include "clang/Basic/SourceManager.h"
19-
#include "clang/Format/Format.h"
20-
#include "llvm/Support/Regex.h"
16+
#include "TokenAnalyzer.h"
2117

2218
namespace clang {
2319
namespace format {
@@ -28,12 +24,12 @@ FormatTokenLexer::FormatTokenLexer(
2824
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
2925
IdentifierTable &IdentTable)
3026
: FormatTok(nullptr), IsFirstToken(true), StateStack({LexerState::NORMAL}),
31-
Column(Column), TrailingWhitespace(0),
32-
LangOpts(getFormattingLangOpts(Style)), SourceMgr(SourceMgr), ID(ID),
27+
Column(Column), TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID),
3328
Style(Style), IdentTable(IdentTable), Keywords(IdentTable),
3429
Encoding(Encoding), Allocator(Allocator), FirstInLineIndex(0),
3530
FormattingDisabled(false), MacroBlockBeginRegex(Style.MacroBlockBegin),
3631
MacroBlockEndRegex(Style.MacroBlockEnd) {
32+
assert(LangOpts.CPlusPlus);
3733
Lex.reset(new Lexer(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts));
3834
Lex->SetKeepWhitespaceMode(true);
3935

@@ -1442,7 +1438,7 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
14421438

14431439
void FormatTokenLexer::resetLexer(unsigned Offset) {
14441440
StringRef Buffer = SourceMgr.getBufferData(ID);
1445-
LangOpts = getFormattingLangOpts(Style);
1441+
assert(LangOpts.CPlusPlus);
14461442
Lex.reset(new Lexer(SourceMgr.getLocForStartOfFile(ID), LangOpts,
14471443
Buffer.begin(), Buffer.begin() + Offset, Buffer.end()));
14481444
Lex->SetKeepWhitespaceMode(true);

clang/lib/Format/FormatTokenLexer.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,9 @@
1717

1818
#include "Encoding.h"
1919
#include "FormatToken.h"
20-
#include "clang/Basic/LangOptions.h"
21-
#include "clang/Basic/SourceLocation.h"
22-
#include "clang/Basic/SourceManager.h"
23-
#include "clang/Format/Format.h"
2420
#include "llvm/ADT/MapVector.h"
2521
#include "llvm/ADT/SmallPtrSet.h"
2622
#include "llvm/ADT/StringSet.h"
27-
#include "llvm/Support/Regex.h"
2823

2924
#include <stack>
3025

@@ -120,7 +115,6 @@ class FormatTokenLexer {
120115
unsigned Column;
121116
unsigned TrailingWhitespace;
122117
std::unique_ptr<Lexer> Lex;
123-
LangOptions LangOpts;
124118
const SourceManager &SourceMgr;
125119
FileID ID;
126120
const FormatStyle &Style;

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
7979
AffectedRangeManager AffectedRangeMgr(SourceMgr, Env.getCharRanges());
8080

8181
const auto ID = Env.getFileID();
82-
const auto LangOpts = getFormattingLangOpts(Style);
82+
assert(LangOpts.CPlusPlus);
8383
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
8484
Lex.SetCommentRetentionState(true);
8585

clang/lib/Format/TokenAnalyzer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
namespace clang {
3636
namespace format {
3737

38+
LangOptions LangOpts;
39+
3840
// FIXME: Instead of printing the diagnostic we should store it and have a
3941
// better way to return errors through the format APIs.
4042
class FatalDiagnosticConsumer : public DiagnosticConsumer {
@@ -99,9 +101,11 @@ TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
99101

100102
std::pair<tooling::Replacements, unsigned>
101103
TokenAnalyzer::process(bool SkipAnnotation) {
104+
LangOpts = getFormattingLangOpts(Style);
105+
102106
tooling::Replacements Result;
103107
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
104-
IdentifierTable IdentTable(getFormattingLangOpts(Style));
108+
IdentifierTable IdentTable(LangOpts);
105109
FormatTokenLexer Lex(Env.getSourceManager(), Env.getFileID(),
106110
Env.getFirstStartColumn(), Style, Encoding, Allocator,
107111
IdentTable);

clang/lib/Format/TokenAnalyzer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
namespace clang {
3535
namespace format {
3636

37+
extern LangOptions LangOpts;
38+
3739
class Environment {
3840
public:
3941
// This sets up an virtual file system with file \p FileName containing the

clang/lib/Sema/SemaDecl.cpp

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8357,28 +8357,40 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
83578357

83588358
unsigned WarningDiag = diag::warn_decl_shadow;
83598359
SourceLocation CaptureLoc;
8360-
if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
8361-
isa<CXXMethodDecl>(NewDC)) {
8360+
if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
83628361
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
83638362
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
8364-
if (RD->getLambdaCaptureDefault() == LCD_None) {
8365-
// Try to avoid warnings for lambdas with an explicit capture list.
8363+
if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
83668364
const auto *LSI = cast<LambdaScopeInfo>(getCurFunction());
8367-
// Warn only when the lambda captures the shadowed decl explicitly.
8368-
CaptureLoc = getCaptureLocation(LSI, cast<VarDecl>(ShadowedDecl));
8369-
if (CaptureLoc.isInvalid())
8370-
WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8371-
} else {
8372-
// Remember that this was shadowed so we can avoid the warning if the
8373-
// shadowed decl isn't captured and the warning settings allow it.
8365+
if (RD->getLambdaCaptureDefault() == LCD_None) {
8366+
// Try to avoid warnings for lambdas with an explicit capture
8367+
// list. Warn only when the lambda captures the shadowed decl
8368+
// explicitly.
8369+
CaptureLoc = getCaptureLocation(LSI, VD);
8370+
if (CaptureLoc.isInvalid())
8371+
WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8372+
} else {
8373+
// Remember that this was shadowed so we can avoid the warning if
8374+
// the shadowed decl isn't captured and the warning settings allow
8375+
// it.
8376+
cast<LambdaScopeInfo>(getCurFunction())
8377+
->ShadowingDecls.push_back({D, VD});
8378+
return;
8379+
}
8380+
}
8381+
if (isa<FieldDecl>(ShadowedDecl)) {
8382+
// If lambda can capture this, then emit default shadowing warning,
8383+
// Otherwise it is not really a shadowing case since field is not
8384+
// available in lambda's body.
8385+
// At this point we don't know that lambda can capture this, so
8386+
// remember that this was shadowed and delay until we know.
83748387
cast<LambdaScopeInfo>(getCurFunction())
8375-
->ShadowingDecls.push_back(
8376-
{cast<VarDecl>(D), cast<VarDecl>(ShadowedDecl)});
8388+
->ShadowingDecls.push_back({D, ShadowedDecl});
83778389
return;
83788390
}
83798391
}
8380-
8381-
if (cast<VarDecl>(ShadowedDecl)->hasLocalStorage()) {
8392+
if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl);
8393+
VD && VD->hasLocalStorage()) {
83828394
// A variable can't shadow a local variable in an enclosing scope, if
83838395
// they are separated by a non-capturing declaration context.
83848396
for (DeclContext *ParentDC = NewDC;
@@ -8429,19 +8441,28 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
84298441
/// when these variables are captured by the lambda.
84308442
void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) {
84318443
for (const auto &Shadow : LSI->ShadowingDecls) {
8432-
const VarDecl *ShadowedDecl = Shadow.ShadowedDecl;
8444+
const NamedDecl *ShadowedDecl = Shadow.ShadowedDecl;
84338445
// Try to avoid the warning when the shadowed decl isn't captured.
8434-
SourceLocation CaptureLoc = getCaptureLocation(LSI, ShadowedDecl);
84358446
const DeclContext *OldDC = ShadowedDecl->getDeclContext();
8436-
Diag(Shadow.VD->getLocation(), CaptureLoc.isInvalid()
8437-
? diag::warn_decl_shadow_uncaptured_local
8438-
: diag::warn_decl_shadow)
8439-
<< Shadow.VD->getDeclName()
8440-
<< computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8441-
if (!CaptureLoc.isInvalid())
8442-
Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8443-
<< Shadow.VD->getDeclName() << /*explicitly*/ 0;
8444-
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8447+
if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
8448+
SourceLocation CaptureLoc = getCaptureLocation(LSI, VD);
8449+
Diag(Shadow.VD->getLocation(),
8450+
CaptureLoc.isInvalid() ? diag::warn_decl_shadow_uncaptured_local
8451+
: diag::warn_decl_shadow)
8452+
<< Shadow.VD->getDeclName()
8453+
<< computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8454+
if (CaptureLoc.isValid())
8455+
Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8456+
<< Shadow.VD->getDeclName() << /*explicitly*/ 0;
8457+
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8458+
} else if (isa<FieldDecl>(ShadowedDecl)) {
8459+
Diag(Shadow.VD->getLocation(),
8460+
LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
8461+
: diag::warn_decl_shadow_uncaptured_local)
8462+
<< Shadow.VD->getDeclName()
8463+
<< computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8464+
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8465+
}
84458466
}
84468467
}
84478468

0 commit comments

Comments
 (0)