简体   繁体   English

gcc 标志来检测字符串文字连接?

[英]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.即有人忘记了 , 在“string3”和“string3”和“string4”被粘贴在一起之后。 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 ?现在,虽然这是完全合法的代码,但是否有 gcc 警告标志或其他工具可以扫描代码库中的类似错误?

A basic 'tool' you could use is a preprocessor hack, but it's a very ugly solution:您可以使用的基本“工具”是预处理器 hack,但这是一个非常丑陋的解决方案:

#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: GCC有这样一个警告:

-Wtraditional (C and Objective-C only) -Wtraditional(仅限 C 和 Objective-C)

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.警告某些在传统 C 和 ISO C 中表现不同的构造。同时警告没有传统 C 等效项的 ISO C 构造,和/或应避免的有问题的构造。

..... .....

  • Usage of ISO string concatenation is detected.检测到 ISO 字符串连接的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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