Skip to content

Commit e77a856

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 271a63f commit e77a856

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
@@ -5182,3 +5182,14 @@ def NonString : InheritableAttr {
51825182
let Subjects = SubjectList<[Var, Field]>;
51835183
let Documentation = [NonStringDocs];
51845184
}
5185+
5186+
def ModularFormat : InheritableAttr {
5187+
let Spellings = [Clang<"modular_format">];
5188+
let Args = [
5189+
IdentifierArgument<"ModularImplFn">,
5190+
StringArgument<"ImplName">,
5191+
VariadicStringArgument<"Aspects">
5192+
];
5193+
let Subjects = SubjectList<[Function]>;
5194+
let Documentation = [ModularFormatDocs];
5195+
}

clang/include/clang/Basic/AttrDocs.td

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9427,3 +9427,28 @@ diagnostics with code like:
94279427
__attribute__((nonstring)) char NotAStr[3] = "foo"; // Not diagnosed
94289428
}];
94299429
}
9430+
9431+
def ModularFormatDocs : Documentation {
9432+
let Category = DocCatFunction;
9433+
let Content = [{
9434+
The ``modular_format`` attribute can be applied to a function that bears the
9435+
``format`` attribute to indicate that the implementation is modular on the
9436+
format string argument. When the format argument for a given call is constant,
9437+
the compiler may redirect the call to the symbol given as the first argument to
9438+
the attribute (the modular implementation function).
9439+
9440+
The second argument is a implementation name, and the remaining arguments are
9441+
aspects of the format string for the compiler to report. If the compiler does
9442+
not understand a aspect, it must summarily report that the format string has
9443+
that aspect.
9444+
9445+
The compiler reports an aspect by issing a relocation for the symbol
9446+
`<impl_name>_<aspect>``. This arranges for code and data needed to support the
9447+
aspect of the implementation to be brought into the link to satisfy weak
9448+
references in the modular implemenation function.
9449+
9450+
The following aspects are currently supported:
9451+
9452+
- ``float``: The call has a floating point argument
9453+
}];
9454+
}

clang/lib/CodeGen/CGCall.cpp

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

25702570
if (TargetDecl->hasAttr<ArmLocallyStreamingAttr>())
25712571
FuncAttrs.addAttribute("aarch64_pstate_sm_body");
2572+
2573+
if (auto *ModularFormat = TargetDecl->getAttr<ModularFormatAttr>()) {
2574+
// TODO: Error checking
2575+
FormatAttr *Format = TargetDecl->getAttr<FormatAttr>();
2576+
std::string FormatIdx = std::to_string(Format->getFormatIdx());
2577+
std::string FirstArg = std::to_string(Format->getFirstArg());
2578+
SmallVector<StringRef> Args = {
2579+
FormatIdx, FirstArg, ModularFormat->getModularImplFn()->getName(),
2580+
ModularFormat->getImplName()};
2581+
llvm::append_range(Args, ModularFormat->aspects());
2582+
FuncAttrs.addAttribute("modular-format", llvm::join(Args, ","));
2583+
}
25722584
}
25732585

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

clang/lib/Sema/SemaDeclAttr.cpp

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

6900+
static void handleModularFormat(Sema &S, Decl *D, const ParsedAttr &AL) {
6901+
StringRef ImplName;
6902+
if (!S.checkStringLiteralArgumentAttr(AL, 1, ImplName))
6903+
return;
6904+
SmallVector<StringRef> Aspects;
6905+
for (unsigned I = 2, E = AL.getNumArgs(); I != E; ++I) {
6906+
StringRef Aspect;
6907+
if (!S.checkStringLiteralArgumentAttr(AL, I, Aspect))
6908+
return;
6909+
Aspects.push_back(Aspect);
6910+
}
6911+
6912+
// Store aspects sorted and without duplicates.
6913+
llvm::sort(Aspects);
6914+
Aspects.erase(llvm::unique(Aspects), Aspects.end());
6915+
6916+
// TODO: Type checking on identifier
6917+
// TODO: Merge attributes
6918+
D->addAttr(::new (S.Context) ModularFormatAttr(
6919+
S.Context, AL, AL.getArgAsIdent(0)->getIdentifierInfo(), ImplName,
6920+
Aspects.data(), Aspects.size()));
6921+
}
6922+
69006923
//===----------------------------------------------------------------------===//
69016924
// Top Level Sema Entry Points
69026925
//===----------------------------------------------------------------------===//
@@ -7821,6 +7844,10 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
78217844
case ParsedAttr::AT_VTablePointerAuthentication:
78227845
handleVTablePointerAuthentication(S, D, AL);
78237846
break;
7847+
7848+
case ParsedAttr::AT_ModularFormat:
7849+
handleModularFormat(S, D, AL);
7850+
break;
78247851
}
78257852
}
78267853

0 commit comments

Comments
 (0)