Commit 06b8a40f8b5eb1188a326d90705945b3a9b6a19f

Patrick Steinhardt 2018-02-16T11:29:46

Explicitly mark fallthrough cases with comments A lot of compilers nowadays generate warnings when there are cases in a switch statement which implicitly fall through to the next case. To avoid this warning, the last line in the case that is falling through can have a comment matching a regular expression, where one possible comment body would be `/* fall through */`. An alternative to the comment would be an explicit attribute like e.g. `[[clang::fallthrough]` or `__attribute__ ((fallthrough))`. But GCC only introduced support for such an attribute recently with GCC 7. Thus, and also because the fallthrough comment is supported by most compilers, we settle for using comments instead. One shortcoming of that method is that compilers are very strict about that. Most interestingly, that comment _really_ has to be the last line. In case a closing brace follows the comment, the heuristic will fail.

diff --git a/src/patch_parse.c b/src/patch_parse.c
index 27c01e9..acdd45e 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -575,6 +575,7 @@ static int parse_hunk_body(
 		switch (c) {
 		case '\n':
 			prefix = 0;
+			/* fall through */
 
 		case ' ':
 			origin = GIT_DIFF_LINE_CONTEXT;
diff --git a/src/revparse.c b/src/revparse.c
index 4ab4fb9..7cb22f4 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -770,7 +770,6 @@ int revparse__ext(
 		}
 
 		case '@':
-		{
 			if (spec[pos+1] == '{') {
 				git_object *temp_object = NULL;
 
@@ -786,10 +785,8 @@ int revparse__ext(
 				if (temp_object != NULL)
 					base_rev = temp_object;
 				break;
-			} else {
-				/* Fall through */
 			}
-		}
+			/* fall through */
 
 		default:
 			if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0)
diff --git a/src/tree.c b/src/tree.c
index 6a136a6..fdf36f8 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -957,7 +957,7 @@ int git_tree_entry_bypath(
 		 * walking down the path */
 		if (path[filename_len + 1] != '\0')
 			break;
-
+		/* fall through */
 	case '\0':
 		/* If there are no more components in the path, return
 		 * this entry */
diff --git a/src/util.c b/src/util.c
index 1760a31..b984b99 100644
--- a/src/util.c
+++ b/src/util.c
@@ -478,9 +478,11 @@ uint32_t git__hash(const void *key, int len, uint32_t seed)
 
 	switch(len & 3) {
 	case 3: k1 ^= tail[2] << 16;
+		/* fall through */
 	case 2: k1 ^= tail[1] << 8;
+		/* fall through */
 	case 1: k1 ^= tail[0];
-			MURMUR_BLOCK();
+		MURMUR_BLOCK();
 	}
 
 	h1 ^= len;