Bug 1928633 - Better hinting for global variable linter r=arai

Differential Revision: https://phabricator.services.mozilla.com/D228991
This commit is contained in:
serge-sans-paille 2024-11-14 15:24:37 +00:00
parent 377f4a1431
commit 5df06598e8
2 changed files with 17 additions and 3 deletions

View File

@ -31,8 +31,14 @@ void GlobalVariableInitializationChecker::check(
const MatchFinder::MatchResult &Result) {
if (const VarDecl *VD =
Result.Nodes.getNodeAs<VarDecl>("flagged-constinit_global")) {
diag(VD->getBeginLoc(), "Global variable flagged as MOZ_RUNINIT but actually has constant initialisation. Consider flagging it as constexpr or MOZ_CONSTINIT instead.",
DiagnosticIDs::Error);
if (VD->hasConstantInitialization()) {
diag(VD->getBeginLoc(), "Global variable flagged as MOZ_RUNINIT but actually has constinit initialisation. Consider flagging it as constexpr or MOZ_CONSTINIT instead.",
DiagnosticIDs::Error);
}
else {
diag(VD->getBeginLoc(), "Global variable flagged as MOZ_RUNINIT but actually has constant initialisation. Consider removing the annotation or (as a last resort) flagging it as MOZ_GLOBINIT.",
DiagnosticIDs::Error);
}
}
if (const VarDecl *VD =

View File

@ -43,7 +43,15 @@ struct InvalidRuninitGlobal {
constexpr InvalidRuninitGlobal() {}
};
MOZ_RUNINIT InvalidRuninitGlobal g5; // expected-error {{Global variable flagged as MOZ_RUNINIT but actually has constant initialisation. Consider flagging it as constexpr or MOZ_CONSTINIT instead.}}
MOZ_RUNINIT InvalidRuninitGlobal g5; // expected-error {{Global variable flagged as MOZ_RUNINIT but actually has constinit initialisation. Consider flagging it as constexpr or MOZ_CONSTINIT instead.}}
constexpr InvalidRuninitGlobal g5a;
struct InvalidRuninitGlobal2 {
int i;
};
MOZ_RUNINIT InvalidRuninitGlobal2 g5b; // expected-error {{Global variable flagged as MOZ_RUNINIT but actually has constant initialisation. Consider removing the annotation or (as a last resort) flagging it as MOZ_GLOBINIT.}}
InvalidRuninitGlobal2 g5c;
// Static variable with extern constructor
Global g6; // expected-error {{Global variable has runtime initialisation, try to remove it, make it constexpr or MOZ_CONSTINIT if possible, or as a last resort flag it as MOZ_RUNINIT.}}