Skip to content

Commit a620d12

Browse files
committed
merge main into amd-staging
2 parents 51573fd + 68e74f8 commit a620d12

File tree

98 files changed

+3422
-786
lines changed

Some content is hidden

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

98 files changed

+3422
-786
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace clang::tidy::cppcoreguidelines {
1717
AvoidNonConstGlobalVariablesCheck::AvoidNonConstGlobalVariablesCheck(
1818
StringRef Name, ClangTidyContext *Context)
1919
: ClangTidyCheck(Name, Context),
20-
AllowInternalLinkage(Options.get("AllowInternalLinkage", false)) {}
20+
AllowInternalLinkage(Options.get("AllowInternalLinkage", false)),
21+
AllowThreadLocal(Options.get("AllowThreadLocal", false)) {}
2122

2223
void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {
2324
auto NamespaceMatcher = AllowInternalLinkage
@@ -31,6 +32,8 @@ void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {
3132
GlobalContext,
3233
AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))
3334
: varDecl(),
35+
AllowThreadLocal ? varDecl(unless(hasThreadStorageDuration()))
36+
: varDecl(),
3437
unless(anyOf(
3538
isConstexpr(), hasType(isConstQualified()),
3639
hasType(referenceType())))); // References can't be changed, only the

clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AvoidNonConstGlobalVariablesCheck : public ClangTidyCheck {
2727

2828
private:
2929
const bool AllowInternalLinkage;
30+
const bool AllowThreadLocal;
3031
};
3132

3233
} // namespace clang::tidy::cppcoreguidelines

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ Changes in existing checks
331331
an additional matcher that generalizes the copy-and-swap idiom pattern
332332
detection.
333333

334+
- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
335+
<clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check
336+
by adding a new option `AllowThreadLocal` that suppresses warnings on
337+
non-const global variables with thread-local storage duration.
338+
334339
- Improved :doc:`cppcoreguidelines-init-variables
335340
<clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
336341
insertion location for function pointers with multiple parameters.

clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ Options
4949

5050
When set to `true`, static non-const variables and variables in anonymous
5151
namespaces will not generate a warning. The default value is `false`.
52+
53+
.. option:: AllowThreadLocal
54+
55+
When set to `true`, non-const global variables with thread-local storage
56+
duration will not generate a warning. The default value is `false`.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %check_clang_tidy %s -check-suffixes=,DEFAULT cppcoreguidelines-avoid-non-const-global-variables %t
22
// RUN: %check_clang_tidy %s -check-suffixes=,INTERNAL-LINKAGE cppcoreguidelines-avoid-non-const-global-variables %t -- \
33
// RUN: -config="{CheckOptions: {cppcoreguidelines-avoid-non-const-global-variables.AllowInternalLinkage : 'true'}}"
4+
// RUN: %check_clang_tidy %s -check-suffixes=,THREAD-LOCAL cppcoreguidelines-avoid-non-const-global-variables %t -- \
5+
// RUN: -config="{CheckOptions: {cppcoreguidelines-avoid-non-const-global-variables.AllowThreadLocal : 'true'}}"
46

57
int nonConstInt = 0;
68
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'nonConstInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
@@ -42,14 +44,23 @@ namespace {
4244
int nonConstAnonymousNamespaceInt = 0;
4345
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:5: warning: variable 'nonConstAnonymousNamespaceInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
4446
// CHECK-MESSAGES-INTERNAL-LINKAGE-NOT: :[[@LINE-2]]:5: warning: variable 'nonConstAnonymousNamespaceInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
47+
// CHECK-MESSAGES-THREAD-LOCAL: :[[@LINE-3]]:5: warning: variable 'nonConstAnonymousNamespaceInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
4548
} // namespace
4649

4750
static int nonConstStaticInt = 0;
4851
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:12: warning: variable 'nonConstStaticInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
4952
// CHECK-MESSAGES-INTERNAL-LINKAGE-NOT: :[[@LINE-2]]:12: warning: variable 'nonConstStaticInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
53+
// CHECK-MESSAGES-THREAD-LOCAL: :[[@LINE-3]]:12: warning: variable 'nonConstStaticInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
5054

5155
static const int constStaticInt = 0;
5256

57+
thread_local int threadLocalInt = 0;
58+
// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:18: warning: variable 'threadLocalInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
59+
// CHECK-MESSAGES-INTERNAL-LINKAGE: :[[@LINE-2]]:18: warning: variable 'threadLocalInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
60+
// CHECK-MESSAGES-THREAD-LOCAL-NOT: :[[@LINE-3]]:18: warning: variable 'threadLocalInt' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
61+
62+
thread_local const int threadLocalConstInt = 0;
63+
5364
class DummyClass {
5465
public:
5566
int nonConstPublicMemberVariable = 0;
@@ -137,6 +148,7 @@ DummyEnum nonConstAnonymousNamespaceEnumInstance = DummyEnum::first;
137148
}
138149
// CHECK-MESSAGES-DEFAULT: :[[@LINE-2]]:11: warning: variable 'nonConstAnonymousNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
139150
// CHECK-MESSAGES-INTERNAL-LINKAGE-NOT: :[[@LINE-2]]:11: warning: variable 'nonConstAnonymousNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
151+
// CHECK-MESSAGES-THREAD-LOCAL: :[[@LINE-4]]:11: warning: variable 'nonConstAnonymousNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
140152

141153
// CHECKING FOR NON-CONST GLOBAL STRUCT ///////////////////////////////////////
142154
struct DummyStruct {
@@ -181,6 +193,7 @@ DummyStruct nonConstAnonymousNamespaceStructInstance;
181193
}
182194
// CHECK-MESSAGES-DEFAULT: :[[@LINE-2]]:13: warning: variable 'nonConstAnonymousNamespaceStructInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
183195
// CHECK-MESSAGES-INTERNAL-LINKAGE-NOT: :[[@LINE-2]]:11: warning: variable 'nonConstAnonymousNamespaceEnumInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
196+
// CHECK-MESSAGES-THREAD-LOCAL: :[[@LINE-4]]:13: warning: variable 'nonConstAnonymousNamespaceStructInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
184197

185198
// CHECKING FOR NON-CONST GLOBAL UNION ////////////////////////////////////////
186199
union DummyUnion {
@@ -222,6 +235,7 @@ DummyUnion nonConstAnonymousNamespaceUnionInstance = {0x0};
222235
}
223236
// CHECK-MESSAGES-DEFAULT: :[[@LINE-2]]:12: warning: variable 'nonConstAnonymousNamespaceUnionInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
224237
// CHECK-MESSAGES-INTERNAL-LINKAGE-NOT: :[[@LINE-3]]:12: warning: variable 'nonConstAnonymousNamespaceUnionInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
238+
// CHECK-MESSAGES-THREAD-LOCAL: :[[@LINE-4]]:12: warning: variable 'nonConstAnonymousNamespaceUnionInstance' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]
225239

226240
// CHECKING FOR NON-CONST GLOBAL FUNCTION POINTER /////////////////////////////
227241
int dummyFunction() {

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6144,7 +6144,6 @@ def nofixprebinding : Flag<["-"], "nofixprebinding">;
61446144
def nolibc : Flag<["-"], "nolibc">;
61456145
def nomultidefs : Flag<["-"], "nomultidefs">;
61466146
def nopie : Flag<["-"], "nopie">, Visibility<[ClangOption, FlangOption]>, Flags<[TargetSpecific]>; // OpenBSD
6147-
def no_pie : Flag<["-"], "no-pie">, Visibility<[ClangOption, FlangOption]>;
61486147
def noprebind : Flag<["-"], "noprebind">;
61496148
def noprofilelib : Flag<["-"], "noprofilelib">;
61506149
def noseglinkedit : Flag<["-"], "noseglinkedit">;
@@ -6258,7 +6257,6 @@ defm pthread : BoolOption<"", "pthread",
62586257
PosFlag<SetTrue, [], [ClangOption], "Support POSIX threads in generated code">,
62596258
NegFlag<SetFalse>,
62606259
BothFlags<[], [ClangOption, CC1Option, FlangOption, FC1Option]>>;
6261-
def pie : Flag<["-"], "pie">, Group<Link_Group>;
62626260
def static_pie : Flag<["-"], "static-pie">, Group<Link_Group>;
62636261
def read__only__relocs : Separate<["-"], "read_only_relocs">;
62646262
def remap : Flag<["-"], "remap">;
@@ -6660,6 +6658,8 @@ def fpic : Flag<["-"], "fpic">, Group<f_Group>;
66606658
def fno_pic : Flag<["-"], "fno-pic">, Group<f_Group>;
66616659
def fpie : Flag<["-"], "fpie">, Group<f_Group>;
66626660
def fno_pie : Flag<["-"], "fno-pie">, Group<f_Group>;
6661+
def pie : Flag<["-"], "pie">, Group<Link_Group>;
6662+
def no_pie : Flag<["-"], "no-pie">, Group<Link_Group>;
66636663

66646664
} // let Vis = [Default, FlangOption]
66656665

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename,
608608
return FileID::get(LoadedID);
609609
}
610610
unsigned FileSize = File.getSize();
611-
llvm::ErrorOr<bool> NeedConversion =
612-
llvm::needConversion(Filename.str().c_str());
611+
llvm::ErrorOr<bool> NeedConversion = llvm::needConversion(Filename);
613612
if (NeedConversion && *NeedConversion) {
614613
// Buffer size may increase due to potential z/OS EBCDIC to UTF-8
615614
// conversion.

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,14 +1231,13 @@ llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) {
12311231
}
12321232

12331233
llvm::DIType *CGDebugInfo::CreateType(const BitIntType *Ty) {
1234-
12351234
StringRef Name = Ty->isUnsigned() ? "unsigned _BitInt" : "_BitInt";
12361235
llvm::dwarf::TypeKind Encoding = Ty->isUnsigned()
12371236
? llvm::dwarf::DW_ATE_unsigned
12381237
: llvm::dwarf::DW_ATE_signed;
1239-
12401238
return DBuilder.createBasicType(Name, CGM.getContext().getTypeSize(Ty),
1241-
Encoding);
1239+
Encoding, llvm::DINode::FlagZero, 0,
1240+
Ty->getNumBits());
12421241
}
12431242

12441243
llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) {

clang/lib/Driver/Driver.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,18 @@ InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
311311
auto ArgString = A->getAsString(Args);
312312
std::string Nearest;
313313
if (getOpts().findNearest(ArgString, Nearest, VisibilityMask) > 1) {
314-
if (!IsCLMode() &&
315-
getOpts().findExact(ArgString, Nearest,
316-
llvm::opt::Visibility(options::CC1Option))) {
314+
if (IsFlangMode()) {
315+
if (getOpts().findExact(ArgString, Nearest,
316+
llvm::opt::Visibility(options::FC1Option))) {
317+
DiagID = diag::err_drv_unknown_argument_with_suggestion;
318+
Diags.Report(DiagID) << ArgString << "-Xflang " + Nearest;
319+
} else {
320+
DiagID = diag::err_drv_unknown_argument;
321+
Diags.Report(DiagID) << ArgString;
322+
}
323+
} else if (!IsCLMode() && getOpts().findExact(ArgString, Nearest,
324+
llvm::opt::Visibility(
325+
options::CC1Option))) {
317326
DiagID = diag::err_drv_unknown_argument_with_suggestion;
318327
Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest;
319328
} else {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,17 +1453,18 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
14531453
GuardedControlStack = PBP.GuardedControlStack;
14541454
}
14551455

1456-
bool HasPtrauthReturns = llvm::any_of(CmdArgs, [](const char *Arg) {
1457-
return StringRef(Arg) == "-fptrauth-returns";
1458-
});
1456+
Arg *PtrauthReturnsArg = Args.getLastArg(options::OPT_fptrauth_returns,
1457+
options::OPT_fno_ptrauth_returns);
1458+
bool HasPtrauthReturns =
1459+
PtrauthReturnsArg &&
1460+
PtrauthReturnsArg->getOption().matches(options::OPT_fptrauth_returns);
14591461
// GCS is currently untested with ptrauth-returns, but enabling this could be
14601462
// allowed in future after testing with a suitable system.
1461-
if (HasPtrauthReturns &&
1462-
(Scope != "none" || BranchProtectionPAuthLR || GuardedControlStack)) {
1463+
if (Scope != "none" || BranchProtectionPAuthLR || GuardedControlStack) {
14631464
if (Triple.getEnvironment() == llvm::Triple::PAuthTest)
14641465
D.Diag(diag::err_drv_unsupported_opt_for_target)
14651466
<< A->getAsString(Args) << Triple.getTriple();
1466-
else
1467+
else if (HasPtrauthReturns)
14671468
D.Diag(diag::err_drv_incompatible_options)
14681469
<< A->getAsString(Args) << "-fptrauth-returns";
14691470
}
@@ -1709,34 +1710,42 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
17091710

17101711
AddUnalignedAccessWarning(CmdArgs);
17111712

1712-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_intrinsics,
1713-
options::OPT_fno_ptrauth_intrinsics);
1714-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_calls,
1715-
options::OPT_fno_ptrauth_calls);
1716-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_returns,
1717-
options::OPT_fno_ptrauth_returns);
1718-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_auth_traps,
1719-
options::OPT_fno_ptrauth_auth_traps);
1720-
Args.addOptInFlag(
1721-
CmdArgs, options::OPT_fptrauth_vtable_pointer_address_discrimination,
1722-
options::OPT_fno_ptrauth_vtable_pointer_address_discrimination);
1723-
Args.addOptInFlag(
1724-
CmdArgs, options::OPT_fptrauth_vtable_pointer_type_discrimination,
1725-
options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
1726-
Args.addOptInFlag(
1727-
CmdArgs, options::OPT_fptrauth_type_info_vtable_pointer_discrimination,
1728-
options::OPT_fno_ptrauth_type_info_vtable_pointer_discrimination);
1729-
Args.addOptInFlag(
1730-
CmdArgs, options::OPT_fptrauth_function_pointer_type_discrimination,
1731-
options::OPT_fno_ptrauth_function_pointer_type_discrimination);
1732-
1733-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_indirect_gotos,
1734-
options::OPT_fno_ptrauth_indirect_gotos);
1735-
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
1736-
options::OPT_fno_ptrauth_init_fini);
1737-
Args.addOptInFlag(CmdArgs,
1738-
options::OPT_fptrauth_init_fini_address_discrimination,
1739-
options::OPT_fno_ptrauth_init_fini_address_discrimination);
1713+
if (Triple.isOSDarwin() ||
1714+
(Triple.isOSLinux() &&
1715+
Triple.getEnvironment() == llvm::Triple::PAuthTest)) {
1716+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_intrinsics,
1717+
options::OPT_fno_ptrauth_intrinsics);
1718+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_calls,
1719+
options::OPT_fno_ptrauth_calls);
1720+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_returns,
1721+
options::OPT_fno_ptrauth_returns);
1722+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_auth_traps,
1723+
options::OPT_fno_ptrauth_auth_traps);
1724+
Args.addOptInFlag(
1725+
CmdArgs, options::OPT_fptrauth_vtable_pointer_address_discrimination,
1726+
options::OPT_fno_ptrauth_vtable_pointer_address_discrimination);
1727+
Args.addOptInFlag(
1728+
CmdArgs, options::OPT_fptrauth_vtable_pointer_type_discrimination,
1729+
options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
1730+
Args.addOptInFlag(
1731+
CmdArgs, options::OPT_fptrauth_type_info_vtable_pointer_discrimination,
1732+
options::OPT_fno_ptrauth_type_info_vtable_pointer_discrimination);
1733+
Args.addOptInFlag(
1734+
CmdArgs, options::OPT_fptrauth_function_pointer_type_discrimination,
1735+
options::OPT_fno_ptrauth_function_pointer_type_discrimination);
1736+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_indirect_gotos,
1737+
options::OPT_fno_ptrauth_indirect_gotos);
1738+
}
1739+
if (Triple.isOSLinux() &&
1740+
Triple.getEnvironment() == llvm::Triple::PAuthTest) {
1741+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
1742+
options::OPT_fno_ptrauth_init_fini);
1743+
Args.addOptInFlag(
1744+
CmdArgs, options::OPT_fptrauth_init_fini_address_discrimination,
1745+
options::OPT_fno_ptrauth_init_fini_address_discrimination);
1746+
Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_elf_got,
1747+
options::OPT_fno_ptrauth_elf_got);
1748+
}
17401749
Args.addOptInFlag(CmdArgs, options::OPT_faarch64_jump_table_hardening,
17411750
options::OPT_fno_aarch64_jump_table_hardening);
17421751

0 commit comments

Comments
 (0)