瀏覽代碼

Use format attribute on CreateString and FormatString, fix resulting warnings.

Michael Ragazzon 5 年之前
父節點
當前提交
23f7a42981

+ 7 - 0
Include/RmlUi/Core/Platform.h

@@ -126,4 +126,11 @@
     RMLUI_ERRORMSG("Switch case for unhandled ENUM has been hit!  This shouldn't happen!  ENUM Name: " # x); \
     RMLUI_ERRORMSG("Switch case for unhandled ENUM has been hit!  This shouldn't happen!  ENUM Name: " # x); \
     break;
     break;
 
 
+// Tell the compiler of printf-like functions, warns on incorrect usage.
+#if defined __GNUC__ || defined __clang__
+#  define RMLUI_ATTRIBUTE_FORMAT_PRINTF(i, f) __attribute__((format (printf, i, f)))
+#else
+#  define RMLUI_ATTRIBUTE_FORMAT_PRINTF(i, f)
+#endif
+
 #endif
 #endif

+ 2 - 2
Include/RmlUi/Core/StringUtilities.h

@@ -42,10 +42,10 @@ namespace Rml {
 class StringView;
 class StringView;
 
 
 /// Construct a string using sprintf-style syntax.
 /// Construct a string using sprintf-style syntax.
-RMLUICORE_API String CreateString(size_t max_size, const char* format, ...);
+RMLUICORE_API String CreateString(size_t max_size, const char* format, ...) RMLUI_ATTRIBUTE_FORMAT_PRINTF(2,3);
 
 
 /// Format to a string using sprintf-style syntax.
 /// Format to a string using sprintf-style syntax.
-RMLUICORE_API int FormatString(String& string, size_t max_size, const char* format, ...);
+RMLUICORE_API int FormatString(String& string, size_t max_size, const char* format, ...) RMLUI_ATTRIBUTE_FORMAT_PRINTF(3,4);
 
 
 
 
 namespace StringUtilities
 namespace StringUtilities

+ 1 - 1
Samples/basic/treeview/src/FileSystem.cpp

@@ -50,7 +50,7 @@ struct FileSystemNode
 {
 {
 	FileSystemNode(const Rml::String _name, bool _directory, int _depth = -1) : name(_name)
 	FileSystemNode(const Rml::String _name, bool _directory, int _depth = -1) : name(_name)
 	{
 	{
-		id = Rml::CreateString(16, "%x", this);
+		id = Rml::CreateString(32, "%p", (void*)this);
 
 
 		directory = _directory;
 		directory = _directory;
 		depth = _depth;
 		depth = _depth;

+ 2 - 2
Source/Core/DataExpression.cpp

@@ -793,7 +793,7 @@ public:
 		for (size_t i = 0; i < program.size(); i++)
 		for (size_t i = 0; i < program.size(); i++)
 		{
 		{
 			String instruction_str = program[i].data.Get<String>();
 			String instruction_str = program[i].data.Get<String>();
-			str += CreateString(50 + instruction_str.size(), "  %4d  '%c'  %s\n", i, char(program[i].instruction), instruction_str.c_str());
+			str += CreateString(50 + instruction_str.size(), "  %4zu  '%c'  %s\n", i, char(program[i].instruction), instruction_str.c_str());
 		}
 		}
 		return str;
 		return str;
 	}
 	}
@@ -904,7 +904,7 @@ private:
 			if (num_arguments < 0)
 			if (num_arguments < 0)
 				return Error("Invalid number of arguments.");
 				return Error("Invalid number of arguments.");
 			if (stack.size() < size_t(num_arguments))
 			if (stack.size() < size_t(num_arguments))
-				return Error(CreateString(100, "Cannot pop %d arguments, stack contains only %d elements.", num_arguments, stack.size()));
+				return Error(CreateString(100, "Cannot pop %d arguments, stack contains only %zu elements.", num_arguments, stack.size()));
 
 
 			arguments.resize(num_arguments);
 			arguments.resize(num_arguments);
 			for (int i = num_arguments - 1; i >= 0; i--)
 			for (int i = num_arguments - 1; i >= 0; i--)

+ 1 - 1
Source/Core/Elements/DataFormatter.cpp

@@ -42,7 +42,7 @@ DataFormatter::DataFormatter(const String& _name)
 	}
 	}
 	else
 	else
 	{
 	{
-		name = CreateString(64, "%x", this);
+		name = CreateString(64, "%p", (void*)this);
 	}
 	}
 	data_formatters[name] = this;
 	data_formatters[name] = this;
 }
 }

+ 1 - 1
Source/Core/Elements/DataSource.cpp

@@ -49,7 +49,7 @@ DataSource::DataSource(const String& _name)
 	}
 	}
 	else
 	else
 	{
 	{
-		name = CreateString(64, "%x", this);
+		name = CreateString(64, "%p", (void*)this);
 	}
 	}
 	data_sources[name] = this;
 	data_sources[name] = this;
 }
 }

+ 1 - 1
Source/Core/Elements/ElementDataGridRow.cpp

@@ -505,7 +505,7 @@ void ElementDataGridRow::Load(const DataQuery& row_information)
 				}
 				}
 				else if (column->fields[j] == DataSource::NUM_CHILDREN)
 				else if (column->fields[j] == DataSource::NUM_CHILDREN)
 				{
 				{
-					raw_data.push_back(CreateString(8, "%d", children.size()));
+					raw_data.push_back(CreateString(8, "%zu", children.size()));
 					raw_data_total_len += raw_data.back().length();
 					raw_data_total_len += raw_data.back().length();
 				}
 				}
 				else
 				else

+ 3 - 3
Source/Core/Spritesheet.cpp

@@ -136,14 +136,14 @@ size_t SpritesheetList::NumSprites() const
 
 
 String SpritesheetList::ToString() const
 String SpritesheetList::ToString() const
 {
 {
-	String result = CreateString(100, "#SpriteSheets: %d\n", spritesheet_map.size());
+	String result = CreateString(100, "#SpriteSheets: %zu\n", spritesheet_map.size());
 
 
 	for (auto& sheet : spritesheet_map)
 	for (auto& sheet : spritesheet_map)
 	{
 	{
-		result += CreateString(100, "  Sheet '%s'.   #Sprites %d.\n", sheet.first.c_str(), sheet.second->sprite_names.size());
+		result += CreateString(100, "  Sheet '%s'.   #Sprites %zu.\n", sheet.first.c_str(), sheet.second->sprite_names.size());
 	}
 	}
 
 
-	result += CreateString(100, "\n#Sprites: %d\n", sprite_map.size());
+	result += CreateString(100, "\n#Sprites: %zu\n", sprite_map.size());
 	for (auto& sprite : sprite_map)
 	for (auto& sprite : sprite_map)
 	{
 	{
 		result += CreateString(100, "  In '%s': %s\n", sprite.second.sprite_sheet->name.c_str(), sprite.first.c_str());
 		result += CreateString(100, "  In '%s': %s\n", sprite.second.sprite_sheet->name.c_str(), sprite.first.c_str());

+ 7 - 7
Tests/Source/Benchmarks/Element.cpp

@@ -186,13 +186,13 @@ TEST_CASE("Elements (dummy interface)")
 
 
 	const String msg = CreateString(256,
 	const String msg = CreateString(256,
 		"Stats for single Context::Render() with n=%d rows: \n"
 		"Stats for single Context::Render() with n=%d rows: \n"
-		"Render calls: %d\n"
-		"Scissor enable: %d\n"
-		"Scissor set: %d\n"
-		"Texture load: %d\n"
-		"Texture generate: %d\n"
-		"Texture release: %d\n"
-		"Transform set: %d\n",
+		"Render calls: %zu\n"
+		"Scissor enable: %zu\n"
+		"Scissor set: %zu\n"
+		"Texture load: %zu\n"
+		"Texture generate: %zu\n"
+		"Texture release: %zu\n"
+		"Transform set: %zu\n",
 		num_rows,
 		num_rows,
 		counters.render_calls,
 		counters.render_calls,
 		counters.enable_scissor,
 		counters.enable_scissor,