Gcc to detect string literal concatenation?

I recently fixed a bug that was the result of something like

const char *arr[] = { "string1", //some comment "string2", "string3" //another comment "string4", "string5" }; 

i.e. someone forgot, after "string3", and "string3" and "string4" are inserted together. Now, although this is completely legal code, is there a gcc warning flag or other tool that can scan the code base for such errors?

+5
source share
1 answer

The main β€œtool” you can use is preprocessing hacking, but this is a very ugly solution:

 #include <stdlib.h> #include <stdio.h> int start = __LINE__; const char *arr[] = { "string1", //some comment "string2", "string3" //another comment "string4", "string5" };int end = __LINE__; int main(int argc, char **argv){ printf("arr length: %zu\n", sizeof(arr) / sizeof(arr[0])); printf("_assumed_ arr length: %d\n", (end - start - 2)); } 
+1
source

Source: https://habr.com/ru/post/1214242/


All Articles