Bug 445323 - Brace matching does not work correctly with preprocessor disabled sections
Summary: Brace matching does not work correctly with preprocessor disabled sections
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-editor (show other bugs)
Version: 8.3.0   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-29 05:47 EDT by Jan Pohanka CLA
Modified: 2020-09-04 15:19 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Pohanka CLA 2014-09-29 05:47:14 EDT
In following (not very nice) code codan thinks that brace in #if 0 branch matches the last one and thus the function opening brace has no closing counterpart.

Surprisingly no code analysis error is reported.

void test()
{
    int x = 1;
#if 0
    if (x == 1) {
#else
    if (x == 0) {
#endif
        x = 2;
    }
}
Comment 1 Elena Laskavaia CLA 2014-09-29 08:11:42 EDT
Codan does not parse ifdefs, it would be preprocessor.
I am not sure what is the problem, if you don't see any errors
how do you know what "it thinks"?
Comment 2 Jan Pohanka CLA 2014-09-29 08:24:09 EDT
I have tried the same with 8.4 CDT version and the problem is still there.

Steps to reproduce:

1. use test code
void test()
{
    int x = 1;
#if 0
    if (x == 1) {
#else
    if (x == 0) {
#endif
        x = 2;
    }
}

2. move cursor to the function opening bracket and push shortcut to jump to the matching bracket - nothing happens

3. move cursor to the function closing bracket, brace in #if 0 branch is highlighted, jump to the matching brace - voila we are in #if 0 preprocessor branch
Comment 3 Jan Pohanka CLA 2014-09-29 08:25:34 EDT
I forgot to mention that all code following such function has broken bracket matching. That was the reason for me hitting this bug...
Comment 4 Elena Laskavaia CLA 2014-09-30 11:34:37 EDT
Bracket matcher does not even use preprocessor I think, has nothing
to do with codan for sure. I will switch it to editor, I am not sure
where it goes.
Comment 5 Nathan Ridge CLA 2017-01-18 23:51:29 EST
Bug 222032 is similar (it concerns the highlighting of the matching brace, not navigation to it, but presumably those use the same analysis under the hood).

Personally, code where a preprocessor branch covers one of a pair of matching braces but not another, seems very messy to me. I rarely if ever encounter code like this in the codebases I work on.
Comment 6 Jan Pohanka CLA 2017-01-19 02:48:43 EST
I agree that similar code style is ugly and messy, but unfortunately I meet this pretty often in legacy code that I work with... I'm surprised that this behavior is here 2008 (according to linked bug) and there is no more people discussing it :)