[clang] [llvm] [Clang] Show inlining hints for __attribute__((warning/error)) (PR #174892)
Justin Stitt via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 14:47:47 PST 2026
================
@@ -6064,13 +6064,23 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
if (getDebugInfo() && TargetDecl && TargetDecl->hasAttr<MSAllocatorAttr>())
getDebugInfo()->addHeapAllocSiteMetadata(CI, RetTy->getPointeeType(), Loc);
- // Add metadata if calling an __attribute__((error(""))) or warning fn.
- if (TargetDecl && TargetDecl->hasAttr<ErrorAttr>()) {
- llvm::ConstantInt *Line =
- llvm::ConstantInt::get(Int64Ty, Loc.getRawEncoding());
- llvm::ConstantAsMetadata *MD = llvm::ConstantAsMetadata::get(Line);
- llvm::MDTuple *MDT = llvm::MDNode::get(getLLVMContext(), {MD});
- CI->setMetadata("srcloc", MDT);
+ // Add srcloc metadata for [[gnu::error/warning]] diagnostics. Track
+ // inline/static calls for the heuristic fallback when debug info is not
+ // available. This heuristic is conservative and best-effort since static or
+ // inline-annotated functions are still not guaranteed to be inlined.
+ if (TargetDecl) {
+ bool NeedSrcLoc = TargetDecl->hasAttr<ErrorAttr>();
+ if (!NeedSrcLoc) {
+ if (const auto *FD = dyn_cast<FunctionDecl>(TargetDecl))
+ NeedSrcLoc = FD->isInlined() || FD->hasAttr<AlwaysInlineAttr>() ||
+ FD->getStorageClass() == SC_Static ||
+ FD->isInAnonymousNamespace();
----------------
JustinStitt wrote:
> This is imposing a non-zero cost on all code, whether or not it uses ErrorAttr. Imposing a cost on everyone for the sake of the Linux kernel isn't great.
The cost is a dyn_cast-check and some short-circuitable trivial checks. The most expensive check after the dynamic cast is probably the `hasAttr` check. I don't see how I can avoid it :(
> How much does this cost? Do we expect diagnostics that are relevant outside the Linux kernel to use this?
I put some benchmarks I ran a couple of weeks ago in the PR description.
https://github.com/llvm/llvm-project/pull/174892
More information about the cfe-commits
mailing list