================
@@ -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()))
----------------
cor3ntin wrote:

I have no opinion on this feature, however, we should make sure the 
documentation matches the feature - the restriction on string literal is not 
documented anywhere.

https://github.com/llvm/llvm-project/pull/187986
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to