之后preprocessing解析C ++源文件 [英] Parsing a C++ source file after preprocessing

查看:104
本文介绍了之后preprocessing解析C ++源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的定制分析器来分析 C ++ 文件(写在 C ++ )。开始分析之前,我会想摆脱所有的的#define 。我想要的源文件是后preprocessing编译。所以,最好的办法将是对该文件运行çpreprocessor ​​

I am trying to analyze c++ files using my custom made parser (written in c++). Before start parsing, I will like to get rid of all #define. I want the source file to be compilable after preprocessing. So best way will be to run C Preprocessor on the file.

cpp myfile.cpp temp.cpp
// or
g++ -E myfile.cpp > templ.cpp

[最新建议是值得欢迎的。]

[New suggestions are welcome.]

但由于此,原线和它们的行号将丢失的文件将包含所有的标题信息也和欲保留的行号。所以从我决定的方式是,

But due to this, the original lines and their line numbers will be lost as the file will contain all the header information also and I want to retain the line numbers. So the way out I have decided is,


  1. 添加一个特殊的符号前
    在源文件中的每一行(除了preprocessors)

  2. 运行preprocessor ​​

  3. 提取与特殊行
    符号并对其进行分析

例如,一个典型的源文件看起来像:

For example, a typical source file will look like:

#include<iostream>
#include"xyz.h"
int x;    
#define SOME value
/*
**  This is a test file
*/
typedef char* cp;

void myFunc (int* i, ABC<int, X<double> > o)
{
  //...
}

class B {
};

添加符号之后,它就会像,

After adding symbol it will be like,

#include<iostream>
#include"xyz.h"
@3@int x;    
#define SOME value
@5@/*
@6@**  This is a test file
@7@*/
@8@typedef char* cp;
@9@
@10@void myFunc (int* i, ABC<int, X<double> > o)
@11@{
@12@  //...
@13@}
@14@
@15@class B {
@16@};

一旦所有的宏和评论将被删除,我会留下数千行中几百将原来的源$ C ​​$ C。

Once all the macros and comments are removed, I will be left with thousands of line in which few hundred will be the original source code.

是这种做法是否正确?我缺少任何一个角落呢?

推荐答案

您知道,G ++ -E增加了一些自有线路输出,用来指示在原始文件的行号?你会发现像

You realize that g++ -E adds some of its own lines to its output which indicate line numbers in the original file? You'll find lines like

# 2 "foo.cc" 2

这表明,你正在看文件foo.cc的2行。每当线中的常规序列被破坏这些行被插入

which indicate that you're looking at line 2 of file foo.cc . These lines are inserted whenever the regular sequence of lines is disrupted.

这篇关于之后preprocessing解析C ++源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆