Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

c++ - Is there a way how to skip line numbers for a block of code in gcc/clang?

I am working on a static runtime analysis tool which inserts some instrumentation code into each function before compiling it. The problem is due the newly inserted lines, all line numbers in compiler errors are wrong. Because I would like this tool to be as transparent as possible, the user should not be forced to read the modified file or to guess the true locations of the errors.

I am aware of #line but it only supports setting the line number to an absolute value and I currently do not know those numbers.

What I would like to is transfrom this

void foo(int x){                        //line 3
    if(x % 2 == 0)                      //line 4
        std::cout << "Even argument
"; //line 5
    else                                //line 6
        std::cout << "Odd argument
";  //line 7
}

into this

void foo(int x){                        //line 3
    {
        //Instrumentation
    }
#line ???
    if(x % 2 == 0)                      //line 4 - I want this to still be 4
        std::cout << "Even argument
"; //line 5
    else                                //line 6
        std::cout << "Odd argument
";  //line 7
}

I only managed to revert the line numbering back by one number with

#line __LINE__

which I do not even know whether it is guaranteed to work. So I theoretically can pack the instrumentation code into one line and it would work I guess. Although the end user won't read it, I will. So I would prefer not making the life too difficult for myself.

I can compute the height of the added code if it helps, but e.g.

#line (__LINE__ - 4)

does not work either because #line expects a positive integer.

question from:https://stackoverflow.com/questions/65909689/is-there-a-way-how-to-skip-line-numbers-for-a-block-of-code-in-gcc-clang

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are looking for the #line pre-processor directive where you can specify the line. See here: https://en.cppreference.com/w/cpp/preprocessor/line


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...