Skip to content

Commit a9ac228

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 c0038c2 commit a9ac228

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
@@ -5290,3 +5290,14 @@ def NonString : InheritableAttr {
52905290
let Subjects = SubjectList<[Var, Field]>;
52915291
let Documentation = [NonStringDocs];
52925292
}
5293+
5294+
def ModularFormat : InheritableAttr {
5295+
let Spellings = [Clang<"modular_format">];
5296+
let Args = [
5297+
IdentifierArgument<"ModularImplFn">,
5298+
StringArgument<"ImplName">,
5299+
VariadicStringArgument<"Aspects">
5300+
];
5301+
let Subjects = SubjectList<[Function]>;
5302+
let Documentation = [ModularFormatDocs];
5303+
}

clang/include/clang/Basic/AttrDocs.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9635,3 +9635,28 @@ silence diagnostics with code like:
96359635
__attribute__((nonstring)) char NotAStr[3] = "foo"; // Not diagnosed
96369636
}];
96379637
}
9638+
9639+
def ModularFormatDocs : Documentation {
9640+
let Category = DocCatFunction;
9641+
let Content = [{
9642+
The ``modular_format`` attribute can be applied to a function that bears the
9643+
``format`` attribute to indicate that the implementation is modular on the
9644+
format string argument. When the format argument for a given call is constant,
9645+
the compiler may redirect the call to the symbol given as the first argument to
9646+
the attribute (the modular implementation function).
9647+
9648+
The second argument is a implementation name, and the remaining arguments are
9649+
aspects of the format string for the compiler to report. If the compiler does
9650+
not understand a aspect, it must summarily report that the format string has
9651+
that aspect.
9652+
9653+
The compiler reports an aspect by issing a relocation for the symbol
9654+
`<impl_name>_<aspect>``. This arranges for code and data needed to support the
9655+
aspect of the implementation to be brought into the link to satisfy weak
9656+
references in the modular implemenation function.
9657+
9658+
The following aspects are currently supported:
9659+
9660+
- ``float``: The call has a floating point argument
9661+
}];
9662+
}

clang/lib/CodeGen/CGCall.cpp

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

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

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

clang/lib/Sema/SemaDeclAttr.cpp

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

6786+
static void handleModularFormat(Sema &S, Decl *D, const ParsedAttr &AL) {
6787+
StringRef ImplName;
6788+
if (!S.checkStringLiteralArgumentAttr(AL, 1, ImplName))
6789+
return;
6790+
SmallVector<StringRef> Aspects;
6791+
for (unsigned I = 2, E = AL.getNumArgs(); I != E; ++I) {
6792+
StringRef Aspect;
6793+
if (!S.checkStringLiteralArgumentAttr(AL, I, Aspect))
6794+
return;
6795+
Aspects.push_back(Aspect);
6796+
}
6797+
6798+
// Store aspects sorted and without duplicates.
6799+
llvm::sort(Aspects);
6800+
Aspects.erase(llvm::unique(Aspects), Aspects.end());
6801+
6802+
// TODO: Type checking on identifier
6803+
// TODO: Merge attributes
6804+
D->addAttr(::new (S.Context) ModularFormatAttr(
6805+
S.Context, AL, AL.getArgAsIdent(0)->getIdentifierInfo(), ImplName,
6806+
Aspects.data(), Aspects.size()));
6807+
}
6808+
67866809
//===----------------------------------------------------------------------===//
67876810
// Top Level Sema Entry Points
67886811
//===----------------------------------------------------------------------===//
@@ -7711,6 +7734,10 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
77117734
case ParsedAttr::AT_VTablePointerAuthentication:
77127735
handleVTablePointerAuthentication(S, D, AL);
77137736
break;
7737+
7738+
case ParsedAttr::AT_ModularFormat:
7739+
handleModularFormat(S, D, AL);
7740+
break;
77147741
}
77157742
}
77167743

0 commit comments

Comments
 (0)