Skip to content

Commit 4c53b27

Browse files
committed
[clang] "modular_format" attribute for functions using format strings
This provides a C language version of the new IR modular-format attribute. This, in concert with the format attribute, allows a library function to declare that a modular version of its implementation is available. See issue #146159 for context.
1 parent 9265309 commit 4c53b27

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5233,3 +5233,14 @@ def NonString : InheritableAttr {
52335233
let Subjects = SubjectList<[Var, Field]>;
52345234
let Documentation = [NonStringDocs];
52355235
}
5236+
5237+
def ModularFormat : InheritableAttr {
5238+
let Spellings = [Clang<"modular_format">];
5239+
let Args = [
5240+
IdentifierArgument<"ModularImplFn">,
5241+
StringArgument<"ImplName">,
5242+
VariadicStringArgument<"Aspects">
5243+
];
5244+
let Subjects = SubjectList<[Function]>;
5245+
let Documentation = [ModularFormatDocs];
5246+
}

clang/include/clang/Basic/AttrDocs.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9549,3 +9549,28 @@ silence diagnostics with code like:
95499549
__attribute__((nonstring)) char NotAStr[3] = "foo"; // Not diagnosed
95509550
}];
95519551
}
9552+
9553+
def ModularFormatDocs : Documentation {
9554+
let Category = DocCatFunction;
9555+
let Content = [{
9556+
The ``modular_format`` attribute can be applied to a function that bears the
9557+
``format`` attribute to indicate that the implementation is modular on the
9558+
format string argument. When the format argument for a given call is constant,
9559+
the compiler may redirect the call to the symbol given as the first argument to
9560+
the attribute (the modular implementation function).
9561+
9562+
The second argument is a implementation name, and the remaining arguments are
9563+
aspects of the format string for the compiler to report. If the compiler does
9564+
not understand a aspect, it must summarily report that the format string has
9565+
that aspect.
9566+
9567+
The compiler reports an aspect by issing a relocation for the symbol
9568+
`<impl_name>_<aspect>``. This arranges for code and data needed to support the
9569+
aspect of the implementation to be brought into the link to satisfy weak
9570+
references in the modular implemenation function.
9571+
9572+
The following aspects are currently supported:
9573+
9574+
- ``float``: The call has a floating point argument
9575+
}];
9576+
}

clang/lib/CodeGen/CGCall.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,18 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
25602560

25612561
if (TargetDecl->hasAttr<ArmLocallyStreamingAttr>())
25622562
FuncAttrs.addAttribute("aarch64_pstate_sm_body");
2563+
2564+
if (auto *ModularFormat = TargetDecl->getAttr<ModularFormatAttr>()) {
2565+
// TODO: Error checking
2566+
FormatAttr *Format = TargetDecl->getAttr<FormatAttr>();
2567+
std::string FormatIdx = std::to_string(Format->getFormatIdx());
2568+
std::string FirstArg = std::to_string(Format->getFirstArg());
2569+
SmallVector<StringRef> Args = {
2570+
FormatIdx, FirstArg, ModularFormat->getModularImplFn()->getName(),
2571+
ModularFormat->getImplName()};
2572+
llvm::append_range(Args, ModularFormat->aspects());
2573+
FuncAttrs.addAttribute("modular-format", llvm::join(Args, ","));
2574+
}
25632575
}
25642576

25652577
// Attach "no-builtins" attributes to:

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6752,6 +6752,29 @@ static void handleVTablePointerAuthentication(Sema &S, Decl *D,
67526752
CustomDiscriminationValue));
67536753
}
67546754

6755+
static void handleModularFormat(Sema &S, Decl *D, const ParsedAttr &AL) {
6756+
StringRef ImplName;
6757+
if (!S.checkStringLiteralArgumentAttr(AL, 1, ImplName))
6758+
return;
6759+
SmallVector<StringRef> Aspects;
6760+
for (unsigned I = 2, E = AL.getNumArgs(); I != E; ++I) {
6761+
StringRef Aspect;
6762+
if (!S.checkStringLiteralArgumentAttr(AL, I, Aspect))
6763+
return;
6764+
Aspects.push_back(Aspect);
6765+
}
6766+
6767+
// Store aspects sorted and without duplicates.
6768+
llvm::sort(Aspects);
6769+
Aspects.erase(llvm::unique(Aspects), Aspects.end());
6770+
6771+
// TODO: Type checking on identifier
6772+
// TODO: Merge attributes
6773+
D->addAttr(::new (S.Context) ModularFormatAttr(
6774+
S.Context, AL, AL.getArgAsIdent(0)->getIdentifierInfo(), ImplName,
6775+
Aspects.data(), Aspects.size()));
6776+
}
6777+
67556778
//===----------------------------------------------------------------------===//
67566779
// Top Level Sema Entry Points
67576780
//===----------------------------------------------------------------------===//
@@ -7679,6 +7702,10 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
76797702
case ParsedAttr::AT_VTablePointerAuthentication:
76807703
handleVTablePointerAuthentication(S, D, AL);
76817704
break;
7705+
7706+
case ParsedAttr::AT_ModularFormat:
7707+
handleModularFormat(S, D, AL);
7708+
break;
76827709
}
76837710
}
76847711

0 commit comments

Comments
 (0)