r198446 - Refactored Builtin::Context::isPrintfLike and isScanfLike into a helper function. The implementations are identical, except for the format arguments being searched for.

Aaron Ballman aaron at aaronballman.com
Fri Jan 3 12:10:54 PST 2014


Author: aaronballman
Date: Fri Jan  3 14:10:54 2014
New Revision: 198446

URL: http://llvm.org/viewvc/llvm-project?rev=198446&view=rev
Log:
Refactored Builtin::Context::isPrintfLike and isScanfLike into a helper function. The implementations are identical, except for the format arguments being searched for.

No functional changes intended.

Modified:
    cfe/trunk/include/clang/Basic/Builtins.h
    cfe/trunk/lib/Basic/Builtins.cpp

Modified: cfe/trunk/include/clang/Basic/Builtins.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Builtins.h?rev=198446&r1=198445&r2=198446&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Builtins.h (original)
+++ cfe/trunk/include/clang/Basic/Builtins.h Fri Jan  3 14:10:54 2014
@@ -177,6 +177,10 @@ private:
   /// \brief Is this builtin supported according to the given language options?
   bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
                           const LangOptions &LangOpts);
+
+  /// \brief Helper function for isPrintfLike and isScanfLike.
+  bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
+              const char *Fmt) const;
 };
 
 }

Modified: cfe/trunk/lib/Basic/Builtins.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Builtins.cpp?rev=198446&r1=198445&r2=198446&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Builtins.cpp (original)
+++ cfe/trunk/lib/Basic/Builtins.cpp Fri Jan  3 14:10:54 2014
@@ -97,40 +97,35 @@ void Builtin::Context::ForgetBuiltin(uns
   Table.get(GetRecord(ID).Name).setBuiltinID(0);
 }
 
-bool
-Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
-                               bool &HasVAListArg) {
-  const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
-  if (!Printf)
+bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
+                              bool &HasVAListArg, const char *Fmt) const {
+  assert(Fmt && "Not passed a format string");
+  assert(::strlen(Fmt) == 2 &&
+         "Format string needs to be two characters long");
+  assert(::toupper(Fmt[0]) == Fmt[1] &&
+         "Format string is not in the form \"xX\"");
+
+  const char *Like = ::strpbrk(GetRecord(ID).Attributes, Fmt);
+  if (!Like)
     return false;
 
-  HasVAListArg = (*Printf == 'P');
+  HasVAListArg = (*Like == Fmt[1]);
 
-  ++Printf;
-  assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
-  ++Printf;
+  ++Like;
+  assert(*Like == ':' && "Format specifier must be followed by a ':'");
+  ++Like;
 
-  assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
-  FormatIdx = strtol(Printf, 0, 10);
+  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
+  FormatIdx = ::strtol(Like, 0, 10);
   return true;
 }
 
-// FIXME: Refactor with isPrintfLike.
-bool
-Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
-                              bool &HasVAListArg) {
-  const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
-  if (!Scanf)
-    return false;
-
-  HasVAListArg = (*Scanf == 'S');
-
-  ++Scanf;
-  assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
-  ++Scanf;
-
-  assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
-  FormatIdx = strtol(Scanf, 0, 10);
-  return true;
+bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
+                                    bool &HasVAListArg) {
+  return isLike(ID, FormatIdx, HasVAListArg, "pP");
 }
 
+bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
+                                   bool &HasVAListArg) {
+  return isLike(ID, FormatIdx, HasVAListArg, "sS");
+}





More information about the cfe-commits mailing list