简体   繁体   English

如何在 C/C++ 的源代码中获取浮点文字的位置?

[英]How to get floating-point literals's locations in source code in C/C++?

I would like to write a function whose input is a piece of C/C++ code, and whose output is the exact of locations of floating-point literals.我想写一个 function ,它的输入是一段 C/C++ 代码,它的 output 是浮点文字的确切位置。 Preferred implementation languages are Java or Python, although this question is language-agnostic.首选实现语言是 Java 或 Python,尽管这个问题是 language-agnostic。

Example input program:示例输入程序:

#include<stdio.h>
#include<string.h>
int main() {
   float x = 10.3;
   int y = 28;
   printf("The float value : %f\n", x);
   printf("The sum of float and int variable : %f\n", (x+y));
   return 0;
}

The output should be something like 4, 13-16 indicating the line and offset location of the float literal 10.3. output 应该类似于4, 13-16表示浮点文字 10.3 的行和偏移位置。 The reason I need to get this location is to automatically transfer the literal to another one for the purpose of developing a bug analysis and tracking tool.我需要获取此位置的原因是为了开发错误分析和跟踪工具,自动将文字转移到另一个位置。

Any idea how we can precisely get the locations?知道我们如何准确地获取位置吗?

I tried to apply a simple regular expression to capture decimals like "10.3" but it would also capture floats in comments, like // APACHE-common-2.0, and strings, like printf( "%2.5g", x);, also regular expression-based solutions would miss floats in "double x=0;".我尝试应用一个简单的正则表达式来捕获像“10.3”这样的小数,但它也会捕获注释中的浮点数,比如 // APACHE-common-2.0 和字符串,比如 printf( "%2.5g", x);,基于正则表达式的解决方案会错过“double x=0;”中的浮点数。

You may be looking for something like Kythe , it is a tool Google build to index and search their source code.您可能正在寻找类似Kythe的东西,它是 Google 构建的用于索引和搜索其源代码的工具。

As I understand it (I have only seen a presentation, not worked with the tool myself), it is build on top of LLVM(?) and uses the compiler to build and extract a graph of the code.据我了解(我只看过一个演示文稿,我自己没有使用该工具),它构建在 LLVM(?)之上,并使用编译器来构建和提取代码图。 This then enables somebody to search for f.ex.然后,这使某人能够搜索 f.ex。 places where a specific function is called - not just functions that are named the same, but call-sites that actually link to "this specific definition".调用特定 function 的地方 - 不仅仅是名称相同的函数,还有实际链接到“这个特定定义”的调用站点。

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

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