[PATCH] D18765: [clang-tidy] Don't complain about const pass_object_size params in declarations

George Burgess IV via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 4 11:19:33 PDT 2016

george.burgess.iv created this revision.
george.burgess.iv added a reviewer: alexfh.
george.burgess.iv added a subscriber: cfe-commits.

This patch seems trivial, but I've never touched clang-tidy before, so I'm just making sure I didn't miss something obvious. :)


Clang has a parameter attribute called `pass_object_size`, which requires that its parameter's type be `const`. This restriction only applies at function definitions. e.g.

void foo(void *p __attribute__((pass_object_size(0))); // this is a decl; const isn't required.
void foo(void *const p __attribute__((pass_object_size(0))) {} // const is required; this is a def.

readability-avoid-const-params-in-decls will complain if you put `const` in the decl. Given that clang gives you an error unless you use `const` in the def, I don't think it's clearly a bad thing to use `const` in the decl. This patch makes said checker not complain about `const` params with the `pass_object_size` attribute.



Index: test/clang-tidy/readability-avoid-const-params-in-decls.cpp
--- test/clang-tidy/readability-avoid-const-params-in-decls.cpp
+++ test/clang-tidy/readability-avoid-const-params-in-decls.cpp
@@ -65,6 +65,8 @@
 void NF6(const int *const) {}
 void NF7(int, const int) {}
 void NF8(const int, const int) {}
+// pass_object_size requires a const pointer param in definitions.
+void NF9(const char *const __attribute__((pass_object_size(0)))) {}
 // Do not match on other stuff
 void NF(const alias_type& i);
@@ -76,3 +78,6 @@
 void NF(const int*);
 void NF(const int* const*);
 void NF(alias_const_type);
+// While pass_object_size only requires a const pointer param in definitions,
+// it's probably best to not complain at users for using it in a decl.
+void NF(const char *const __attribute__((pass_object_size(0))));
Index: clang-tidy/readability/AvoidConstParamsInDecls.cpp
--- clang-tidy/readability/AvoidConstParamsInDecls.cpp
+++ clang-tidy/readability/AvoidConstParamsInDecls.cpp
@@ -31,7 +31,8 @@
 void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
   const auto ConstParamDecl =
-      parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
+      parmVarDecl(hasType(qualType(isConstQualified())),
+                  unless(hasAttr(clang::attr::PassObjectSize))).bind("param");

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18765.52581.patch
Type: text/x-patch
Size: 1635 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160404/f65e9a3a/attachment.bin>

More information about the cfe-commits mailing list