简体   繁体   中英

gcc flag 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"
};

ie 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 ?

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));
}

GCC has such a warning:

-Wtraditional (C and Objective-C only)

Warn about certain constructs that behave differently in traditional and ISO C. Also warn about ISO C constructs that have no traditional C equivalent, and/or problematic constructs that should be avoided.

.....

  • Usage of ISO string concatenation is detected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM