[clang] [llvm] [Clang][AIX] Add -mloadtime-comment-vars flag to preserve identifying variables (PR #187986)
Tony Varghese via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 29 21:21:40 PDT 2026
================
@@ -15338,6 +15339,73 @@ void Sema::CheckThreadLocalForLargeAlignment(VarDecl *VD) {
}
}
+/// Process a variable definition whose mangled name may be listed in
+/// '-mloadtime-comment-vars=': attach an implicit attribute to supported
+/// string variables so CodeGen preserves them as loadtime identifying
+/// strings, and warn when a named variable cannot be preserved.
+static void checkLoadTimeCommentVar(Sema &S, VarDecl *VD) {
+ // Only variables defined at file, namespace, or class scope participate;
+ // anything else (e.g. a function-local static) is silently ignored. A
+ // template has no object-file symbol of its own, only its instantiations
+ // do, so template patterns are ignored as well.
+ if (!VD->isFileVarDecl() && !VD->isStaticDataMember())
+ return;
+ if (VD->isTemplated())
+ return;
+ if (VD->isThisDeclarationADefinition(S.Context) != VarDecl::Definition)
+ return;
+
+ // Only character pointers/arrays with an initializer are supported; a
+ // matched variable of any other form (int, struct, no initializer, ...) is
+ // silently ignored.
+ QualType Ty = VD->getType();
+ const PointerType *PT = Ty->getAs<PointerType>();
+ const ArrayType *AT = PT ? nullptr : S.Context.getAsArrayType(Ty);
+ QualType Pointee = PT ? PT->getPointeeType()
+ : AT ? AT->getElementType()
+ : QualType();
+ if (Pointee.isNull() || !Pointee->isAnyCharacterType() || !VD->hasInit())
+ return;
+
+ // Names are matched against the mangled name, as it appears in the object
+ // file. For plain C file-scope variables this is the source identifier; for
+ // C++ variables it is the mangled symbol.
+ if (!llvm::is_contained(S.getLangOpts().LoadTimeCommentVars,
+ ASTNameGenerator(S.Context).getName(VD)))
+ return;
+
+ // Indices of the %select in warn_loadtime_comment_var_not_preserved.
+ enum { Volatile, BadStorage, DynamicInit, NotStringLiteral };
+ int Reason = -1;
+ if (VD->getStorageDuration() != SD_Static)
+ // The string must have static storage duration; a thread-local variable
+ // is not preserved.
+ Reason = BadStorage;
+ else if (Ty.isVolatileQualified() || Pointee.isVolatileQualified())
+ // A volatile string has no stable value to embed, whether the variable
+ // itself or the character it refers to is volatile-qualified.
+ Reason = Volatile;
+ else if (!VD->hasConstantInitialization())
+ // The string has to be present in the object at load time. A dynamically
+ // initialized variable only gets its value from a startup constructor, so
+ // the object would not contain the intended string.
+ Reason = DynamicInit;
+ else if (PT && !isa<StringLiteral>(VD->getInit()->IgnoreParenImpCasts()))
----------------
tonykuttai wrote:
All three points from this thread are now implemented:
Both consteval calls and User Defined Literals are diagnosed rather than preserved. The identifying string must be present in the object file of the defining TU itself, and only a direct string-literal initializer guarantees that. Both forms have dedicated cases in clang/test/Sema/loadtime-comment-vars.cpp (p_ce, p_udl), pinned to the "not initialized with a string literal" diagnostic.
The element type is now limited to plain char (canonical-type match against Context.CharTy), so signed char, unsigned char, and the wide/Unicode character types do not match and are silently ignored like any other non-matching type. Tests added in clang/test/CodeGen/PowerPC/loadtime-comment-vars.c and clang/test/CodeGen/PowerPC/loadtime-comment-vars.cpp. Also a u8 literal initializing a plain-char variable in C is not specifically rejected and since UTF-8 code units are single-byte this does not affect the intended usage.
Updated the docs to state the expected behavior explicitly. The identifying string is present in the object file compiled from the defining translation unit itself, not merely in the final linked output, along with the plain-char restriction and the list of diagnosed forms.
https://github.com/llvm/llvm-project/pull/187986
More information about the cfe-commits
mailing list