[clang] Warn when unique objects might be duplicated in shared libraries (PR #117622)
Hans Wennborg via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 16 05:15:15 PST 2025
================
@@ -13386,6 +13386,62 @@ void Sema::checkNonTrivialCUnion(QualType QT, SourceLocation Loc,
.visit(QT, nullptr, false);
}
+bool Sema::GloballyUniqueObjectMightBeAccidentallyDuplicated(
+ const VarDecl *dcl) {
+ if (!dcl || !getLangOpts().CPlusPlus)
+ return false;
+
+ // If an object is defined in a source file, its definition can't get
+ // duplicated since it will never appear in more than one TU.
+ if (dcl->getASTContext().getSourceManager().isInMainFile(dcl->getLocation()))
+ return false;
+
+ // We only need to warn if the definition is in a header file, so wait to
+ // diagnose until we've seen the definition.
+ if (!dcl->isThisDeclarationADefinition())
+ return false;
+
+ // If the variable we're looking at is a static local, then we actually care
+ // about the properties of the function containing it.
+ const ValueDecl *target = dcl;
+ // VarDecls and FunctionDecls have different functions for checking
+ // inline-ness, so we have to do it manually.
+ bool target_is_inline = dcl->isInline();
----------------
zmodem wrote:
nit: LLVM style prefers camelCase, even starting with a capital letter for variables (this affects `target` and `dcl` above too)
https://github.com/llvm/llvm-project/pull/117622
More information about the cfe-commits
mailing list