Commit 27a472c6 by Shahbaz Youssefi Committed by Commit Bot

Add compiler printf attribute to relevant functions

This commit includes fixes to undefined behavior caught by this attribute. The following changes have been made: - 0x%0.8p is changed to %016 PRIxPTR. Both 0 and . have undefined behavior with p. Additionally, %p already prints 0x with both gcc and clang. This results in a small output change: void *x = (void *)0x1234; void *y = (void *)0x1234567890abcdef; printf("|%0.8p|\n", x); printf("|%0.8p|\n", y); printf("|%016" PRIxPTR "|\n", reinterpret_cast<uintptr_t>(x)); printf("|%016" PRIxPTR "|\n", reinterpret_cast<uintptr_t>(y)); prints: |0x00001234| |0x1234567890abcdef| |0x0000000000001234| |0x1234567890abcdef| - %d used for GLintptr, GLsizeiptr, EGLTime and EGLnsecsANDROID is changed to %llu and the relevant argument is cast to unsigned long long. This is due to these types being typedefs to unknown types (on Linux for example, these are unsigned long, and my guess would be unsigned long long on Windows where long is 32 bits). - %llu is used for GLuint64, which could be unsigned long (as is on Linux). Those arguments are cast to unsigned long long. - %p is used for some EGLNative types, but those types may not be a pointer. Those arguments are cast to uintptr_t and printed as above. Bug: angleproject:2928 Change-Id: I63e9e998c72701ce8582f1ebf25d6374be9090e4 Reviewed-on: https://chromium-review.googlesource.com/c/1289232 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 's avatarYuly Novikov <ynovikov@chromium.org>
parent 683bb013
...@@ -298,6 +298,34 @@ def just_the_name_packed(param, reserved_set): ...@@ -298,6 +298,34 @@ def just_the_name_packed(param, reserved_set):
else: else:
return name return name
static_cast_to_dict = {
"GLintptr": "unsigned long long",
"GLsizeiptr": "unsigned long long",
"GLuint64": "unsigned long long",
}
reinterpret_cast_to_dict = {
"GLsync": "uintptr_t",
"GLDEBUGPROC": "uintptr_t",
"GLDEBUGPROCKHR": "uintptr_t",
"GLeglImageOES": "uintptr_t",
}
def param_print_argument(param):
name_only = just_the_name(param)
type_only = just_the_type(param)
if "*" in param:
return "reinterpret_cast<uintptr_t>(" + name_only + ")"
if type_only in reinterpret_cast_to_dict:
return "reinterpret_cast<" + reinterpret_cast_to_dict[type_only] + ">(" + name_only + ")"
if type_only in static_cast_to_dict:
return "static_cast<" + static_cast_to_dict[type_only] + ">(" + name_only + ")"
return name_only
format_dict = { format_dict = {
"GLbitfield": "0x%X", "GLbitfield": "0x%X",
"GLboolean": "%u", "GLboolean": "%u",
...@@ -306,22 +334,22 @@ format_dict = { ...@@ -306,22 +334,22 @@ format_dict = {
"GLfixed": "0x%X", "GLfixed": "0x%X",
"GLfloat": "%f", "GLfloat": "%f",
"GLint": "%d", "GLint": "%d",
"GLintptr": "%d", "GLintptr": "%llu",
"GLshort": "%d", "GLshort": "%d",
"GLsizei": "%d", "GLsizei": "%d",
"GLsizeiptr": "%d", "GLsizeiptr": "%llu",
"GLsync": "0x%0.8p", "GLsync": "0x%016\" PRIxPTR \"",
"GLubyte": "%d", "GLubyte": "%d",
"GLuint": "%u", "GLuint": "%u",
"GLuint64": "%llu", "GLuint64": "%llu",
"GLDEBUGPROC": "0x%0.8p", "GLDEBUGPROC": "0x%016\" PRIxPTR \"",
"GLDEBUGPROCKHR": "0x%0.8p", "GLDEBUGPROCKHR": "0x%016\" PRIxPTR \"",
"GLeglImageOES": "0x%0.8p", "GLeglImageOES": "0x%016\" PRIxPTR \"",
} }
def param_format_string(param): def param_format_string(param):
if "*" in param: if "*" in param:
return param + " = 0x%0.8p" return param + " = 0x%016\" PRIxPTR \""
else: else:
type_only = just_the_type(param) type_only = just_the_type(param)
if type_only not in format_dict: if type_only not in format_dict:
...@@ -358,7 +386,7 @@ def format_entry_point_def(cmd_name, proto, params, is_explicit_context): ...@@ -358,7 +386,7 @@ def format_entry_point_def(cmd_name, proto, params, is_explicit_context):
packed_gl_enum_conversions += ["\n " + internal_type + " " + internal_name +" = FromGLenum<" + packed_gl_enum_conversions += ["\n " + internal_type + " " + internal_name +" = FromGLenum<" +
internal_type + ">(" + name + ");"] internal_type + ">(" + name + ");"]
pass_params = [just_the_name(param) for param in params] pass_params = [param_print_argument(param) for param in params]
format_params = [param_format_string(param) for param in params] format_params = [param_format_string(param) for param in params]
return_type = proto[:-len(cmd_name)] return_type = proto[:-len(cmd_name)]
default_return = default_return_value(cmd_name, return_type.strip()) default_return = default_return_value(cmd_name, return_type.strip())
......
...@@ -297,6 +297,16 @@ std::string ToString(const T &value) ...@@ -297,6 +297,16 @@ std::string ToString(const T &value)
#define ANGLE_NOINLINE #define ANGLE_NOINLINE
#endif #endif
#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
#if __has_attribute(format)
#define ANGLE_FORMAT_PRINTF(fmt, args) __attribute__((format(__printf__, fmt, args)))
#else
#define ANGLE_FORMAT_PRINTF(fmt, args)
#endif
#else
#define ANGLE_FORMAT_PRINTF(fmt, args)
#endif
#ifndef ANGLE_STRINGIFY #ifndef ANGLE_STRINGIFY
#define ANGLE_STRINGIFY(x) #x #define ANGLE_STRINGIFY(x) #x
#endif #endif
......
...@@ -31,7 +31,8 @@ namespace gl ...@@ -31,7 +31,8 @@ namespace gl
class ScopedPerfEventHelper : angle::NonCopyable class ScopedPerfEventHelper : angle::NonCopyable
{ {
public: public:
ScopedPerfEventHelper(const char* format, ...); ANGLE_FORMAT_PRINTF(2, 3)
ScopedPerfEventHelper(const char *format, ...);
~ScopedPerfEventHelper(); ~ScopedPerfEventHelper();
}; };
......
...@@ -97,6 +97,7 @@ class VulkanLibrary final : NonCopyable ...@@ -97,6 +97,7 @@ class VulkanLibrary final : NonCopyable
VkInstance mInstance = VK_NULL_HANDLE; VkInstance mInstance = VK_NULL_HANDLE;
}; };
ANGLE_FORMAT_PRINTF(1, 2)
std::string FormatString(const char *fmt, ...) std::string FormatString(const char *fmt, ...)
{ {
va_list vararg; va_list vararg;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "libANGLE/Error.h" #include "libANGLE/Error.h"
#include "libANGLE/RefCountObject.h" #include "libANGLE/RefCountObject.h"
#include <inttypes.h>
#include <stdint.h> #include <stdint.h>
#include <bitset> #include <bitset>
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <sstream> #include <sstream>
#include <stdarg.h> #include <stdarg.h>
ANGLE_FORMAT_PRINTF(1, 2)
static std::vector<char> FormatArg(const char* fmt, ...) static std::vector<char> FormatArg(const char* fmt, ...)
{ {
va_list vararg; va_list vararg;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment