[PATCH] D25266: Add a static_assert to enforce that parameters to llvm::format() are not totally unsafe
Mehdi AMINI via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 6 16:35:29 PDT 2016
This revision was automatically updated to reflect the committed changes.
Closed by commit rL283509: Add a static_assert to enforce that parameters to llvm::format() are not… (authored by mehdi_amini).
Changed prior to commit:
https://reviews.llvm.org/D25266?vs=73723&id=73867#toc
Repository:
rL LLVM
https://reviews.llvm.org/D25266
Files:
llvm/trunk/include/llvm/Support/Format.h
llvm/trunk/lib/Target/PowerPC/PPCVSXSwapRemoval.cpp
Index: llvm/trunk/lib/Target/PowerPC/PPCVSXSwapRemoval.cpp
===================================================================
--- llvm/trunk/lib/Target/PowerPC/PPCVSXSwapRemoval.cpp
+++ llvm/trunk/lib/Target/PowerPC/PPCVSXSwapRemoval.cpp
@@ -962,7 +962,8 @@
DEBUG(dbgs() << format("%6d", ID));
DEBUG(dbgs() << format("%6d", EC->getLeaderValue(ID)));
DEBUG(dbgs() << format(" BB#%3d", MI->getParent()->getNumber()));
- DEBUG(dbgs() << format(" %14s ", TII->getName(MI->getOpcode())));
+ DEBUG(dbgs() << format(" %14s ",
+ TII->getName(MI->getOpcode()).str().c_str()));
if (SwapVector[EntryIdx].IsLoad)
DEBUG(dbgs() << "load ");
Index: llvm/trunk/include/llvm/Support/Format.h
===================================================================
--- llvm/trunk/include/llvm/Support/Format.h
+++ llvm/trunk/include/llvm/Support/Format.h
@@ -75,6 +75,16 @@
/// printed, this synthesizes the string into a temporary buffer provided and
/// returns whether or not it is big enough.
+// Helper to validate that format() parameters are scalars or pointers.
+template <typename... Args> struct validate_format_parameters;
+template <typename Arg, typename... Args>
+struct validate_format_parameters<Arg, Args...> {
+ static_assert(std::is_scalar<Arg>::value,
+ "format can't be used with non fundamental / non pointer type");
+ validate_format_parameters() { validate_format_parameters<Args...>(); }
+};
+template <> struct validate_format_parameters<> {};
+
template <typename... Ts>
class format_object final : public format_object_base {
std::tuple<Ts...> Vals;
@@ -91,7 +101,9 @@
public:
format_object(const char *fmt, const Ts &... vals)
- : format_object_base(fmt), Vals(vals...) {}
+ : format_object_base(fmt), Vals(vals...) {
+ validate_format_parameters<Ts...>();
+ }
int snprint(char *Buffer, unsigned BufferSize) const override {
return snprint_tuple(Buffer, BufferSize, index_sequence_for<Ts...>());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25266.73867.patch
Type: text/x-patch
Size: 2025 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/d995f319/attachment.bin>
More information about the llvm-commits
mailing list