git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] let clang use the constant-return error() macro

From
Jeff King <peff@peff.net>
Date
May 6, 2014, 15:17 UTC
Message-ID
<20140506151750.GB25768@sigill.intra.peff.net>
In-Reply-To
<20140505212938.GA16715@sigill.intra.peff.net>

Commit e208f9c converted error() into a macro to make its constant return value more apparent to calling code. Commit 5ded807 prevents us using this macro with clang, since clang's -Wunused-value is smart enough to realize that the constant "-1" is useless in some contexts.

However, since the last commit puts the constant behind an inline function call, this is enough to prevent the -Wunused-value warning on both modern gcc and clang. So we can now re-enable the macro when compiling with clang.

Tested with clang 3.3, 3.4, and 3.5.
Signed-off-by: Jeff King <peff@peff.net>
---
I still get warnings when compiling with clang -O3, due to
-Warray-bounds. It looks like a bug, though. Clang complains that:
  strcmp(argv[1], "git")

oversteps array bounds when the strcmp is expanded into a mess of builtin magic. So I don't think we are doing anything wrong here.

 cache.h           | 2 +-
 git-compat-util.h | 2 +-
 parse-options.h   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/cache.h b/cache.h
index e2f12b0..35a3e6b 100644
--- a/cache.h
+++ b/cache.h
@@ -1271,7 +1271,7 @@ extern int check_repository_format_version(const char *var, const char *value, v
 extern int git_env_bool(const char *, int);
 extern int git_config_system(void);
 extern int config_error_nonbool(const char *);
-#if defined(__GNUC__) && ! defined(__clang__)
+#if defined(__GNUC__)
 #define config_error_nonbool(s) (config_error_nonbool(s), const_error())
 #endif
 extern const char *get_log_output_encoding(void);
diff --git a/git-compat-util.h b/git-compat-util.h
index b4c437e..70dc028 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -330,7 +330,7 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
  * trying to help gcc, anyway, it's OK; other compilers will fall back to
  * using the function as usual.
  */
-#if defined(__GNUC__) && ! defined(__clang__)
+#if defined(__GNUC__)
 static inline int const_error(void)
 {
 	return -1;
diff --git a/parse-options.h b/parse-options.h
index 2f9be96..7940bc7 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -176,7 +176,7 @@ extern NORETURN void usage_msg_opt(const char *msg,
 
 extern int optbug(const struct option *opt, const char *reason);
 extern int opterror(const struct option *opt, const char *reason, int flags);
-#if defined(__GNUC__) && ! defined(__clang__)
+#if defined(__GNUC__)
 #define opterror(o,r,f) (opterror((o),(r),(f)), const_error())
 #endif
 
-- 
2.0.0.rc1.436.g03cb729
Previous: Jeff KingNext: Felipe Contreras
Message 18 of 26 in “Fix a ton of compiler warnings”
  1. 0/3 Fix a ton of compiler warningsFelipe Contreras, May 4, 2014
  2. 1/3 Revert "make error()'s constant return value more visible"Felipe Contreras, May 4, 2014
  3. Jeff KingMay 5, 2014
  4. Felipe ContrerasMay 5, 2014
  5. Jeff KingMay 5, 2014
  6. Felipe ContrerasMay 5, 2014
  7. Jeff KingMay 5, 2014
  8. Felipe ContrerasMay 5, 2014
  9. Jeff KingMay 5, 2014
  10. 1/2 inline constant return from error() functionJeff King, May 6, 2014
  11. Junio C HamanoMay 6, 2014
  12. Jeff KingMay 7, 2014
  13. Junio C HamanoMay 11, 2014
  14. Jeff KingMay 12, 2014
  15. Felipe ContrerasMay 11, 2014
  16. Jonathan NiederMay 12, 2014
  17. Jeff KingMay 12, 2014
  18. 2/2 let clang use the constant-return error() macroJeff King, May 6, 2014
  19. 2/3 Revert "silence some -Wuninitialized false positives"Felipe Contreras, May 4, 2014
  20. 3/3 Silence a bunch of format-zero-length warningsFelipe Contreras, May 4, 2014
  21. brian m. carlsonMay 4, 2014
  22. Felipe ContrerasMay 4, 2014
  23. Jeff KingMay 5, 2014
  24. Junio C HamanoMay 7, 2014
  25. Heiko VoigtMay 7, 2014
  26. Junio C HamanoMay 7, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.