Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc flag to detect string literal concatenation?

Tags:

c

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 a , after "string3", and "string3" and "string4" gets pasted together. Now, while this is perfectly legal code, is there a gcc warning flag, or other tool that could scan the code base for similar errors ?

like image 684
user1255770 Avatar asked Oct 31 '22 09:10

user1255770


1 Answers

A basic 'tool' you could use is a preprocessor hack, but it's 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));
}
like image 82
Rooke Avatar answered Nov 08 '22 09:11

Rooke