Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct search order of dirs specified via 'sonar.cxx.includeDirectories' #748

Merged
merged 1 commit into from
Jan 8, 2016

Conversation

selltc
Copy link
Contributor

@selltc selltc commented Jan 6, 2016

Previously, the include directories specified via 'sonar.cxx.includeDirectories'
were not necessarily searched in the order specified. This patch corrects the
problem by storing the include paths as a list instead of a hash.

This toy project illustrates the problem, and verifies the fix:

[selltc@mac src]$ l
total 60
drwxr-xr-x  6 selltc selltc  4096 Jan  5 09:08 .
drwxrwxrwt 67 root   root   20480 Jan  5 09:08 ..
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:06 inc1
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:08 inc2
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:08 inc3
-rw-r--r--  1 selltc selltc   172 Jan  5 08:52 sonar-project.properties
-rw-r--r--  1 selltc selltc    30 Jan  5 09:08 x.c
[selltc@mac src]$ cat sonar-project.properties
sonar.projectKey=proj
sonar.projectName=proj
sonar.sources=x.c
sonar.log.level=DEBUG
sonar.cxx.cppcheck.reportPath=cppcheck.xml
sonar.cxx.includeDirectories=inc1,inc2,inc3
[selltc@mac src]$ cat x.c
#include "x.h"

int main() {}
[selltc@mac src]$ l inc1
total 8
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:06 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
[selltc@mac src]$ l inc2
total 12
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:08 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
-rw-r--r-- 1 selltc selltc   38 Jan  5 08:39 x.h
[selltc@mac src]$ l inc3
total 12
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:08 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
-rw-r--r-- 1 selltc selltc   38 Jan  5 08:39 x.h
[selltc@mac src]$ cat inc2/x.h
int f2()
{
    char *p2;
    *p2=0;
}
[selltc@mac src]$ cat inc3/x.h
int f3()
{
    char *p3;
    *p3=0;
}
[selltc@mac src]$ cppcheck --enable=all --xml -Iinc1 -Iinc2 -Iinc3 x.c 2>cppcheck.xml
Checking x.c...
[selltc@mac src]$ cat cppcheck.xml
<?xml version="1.0" encoding="UTF-8"?>
<results>
    <error file="inc2/x.h" line="3" id="unassignedVariable" severity="style" msg="Variable &apos;p2&apos; is not assigned a value."/>
    <error file="inc2/x.h" line="4" id="uninitvar" severity="error" msg="Uninitialized variable: p2"/>
</results>

Running sonar-runner withOUT this patch yields the following output (note the expected search order of inc2 and inc3 is reversed):

...
09:10:46.583 INFO  - Sensor CxxSquidSensor
09:10:46.589 DEBUG - setIncludeDirectories() adding dir 'inc1'
09:10:46.589 DEBUG - setIncludeDirectories() adding dir 'inc2'
09:10:46.590 DEBUG - setIncludeDirectories() adding dir 'inc3'
09:10:46.644 DEBUG - storing include root: '/var/tmp/src/inc1'
09:10:46.645 DEBUG - storing include root: '/var/tmp/src/inc3'
09:10:46.645 DEBUG - storing include root: '/var/tmp/src/inc2'
09:10:46.704 DEBUG - [/var/tmp/src/x.c:1]: processing #include "x.h", resolved to file '/var/tmp/src/inc3/x.h'
09:10:46.720 DEBUG - finished preprocessing '/var/tmp/src/x.c'
...

This patch corrects the problem of the include directories being searched in
the wrong order (as the above example shows inc3 being searched before inc2).

Running sonar-runner WITH this patch yields the following output:

09:21:33.507 INFO  - Sensor CxxSquidSensor
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc1'
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc2'
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc3'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc1'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc2'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc3'
09:21:33.603 DEBUG - [/var/tmp/src/x.c:1]: processing #include "x.h", resolved to file '/var/tmp/src/inc2/x.h'

See also #745.

…ies'

Previously, the include directories specified via 'sonar.cxx.includeDirectories'
were not necessarily searched in the order specified.  This patch corrects the
problem by storing the include paths as a list instead of a hash.

This toy project illustrates the problem, and verifies the fix:
```
[selltc@mac src]$ l
total 60
drwxr-xr-x  6 selltc selltc  4096 Jan  5 09:08 .
drwxrwxrwt 67 root   root   20480 Jan  5 09:08 ..
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:06 inc1
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:08 inc2
drwxr-xr-x  2 selltc selltc  4096 Jan  5 09:08 inc3
-rw-r--r--  1 selltc selltc   172 Jan  5 08:52 sonar-project.properties
-rw-r--r--  1 selltc selltc    30 Jan  5 09:08 x.c
[selltc@mac src]$ cat sonar-project.properties
sonar.projectKey=proj
sonar.projectName=proj
sonar.sources=x.c
sonar.log.level=DEBUG
sonar.cxx.cppcheck.reportPath=cppcheck.xml
sonar.cxx.includeDirectories=inc1,inc2,inc3
[selltc@mac src]$ cat x.c
#include "x.h"

int main() {}
[selltc@mac src]$ l inc1
total 8
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:06 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
[selltc@mac src]$ l inc2
total 12
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:08 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
-rw-r--r-- 1 selltc selltc   38 Jan  5 08:39 x.h
[selltc@mac src]$ l inc3
total 12
drwxr-xr-x 2 selltc selltc 4096 Jan  5 09:08 .
drwxr-xr-x 6 selltc selltc 4096 Jan  5 09:08 ..
-rw-r--r-- 1 selltc selltc   38 Jan  5 08:39 x.h
[selltc@mac src]$ cat inc2/x.h
int f2()
{
    char *p2;
    *p2=0;
}
[selltc@mac src]$ cat inc3/x.h
int f3()
{
    char *p3;
    *p3=0;
}
[selltc@mac src]$ cppcheck --enable=all --xml -Iinc1 -Iinc2 -Iinc3 x.c 2>cppcheck.xml
Checking x.c...
[selltc@mac src]$ cat cppcheck.xml
<?xml version="1.0" encoding="UTF-8"?>
<results>
    <error file="inc2/x.h" line="3" id="unassignedVariable" severity="style" msg="Variable &apos;p2&apos; is not assigned a value."/>
    <error file="inc2/x.h" line="4" id="uninitvar" severity="error" msg="Uninitialized variable: p2"/>
</results>
```

Running sonar-runner withOUT this patch yields the following output (note the expected search order of inc2 and inc3 is reversed):
```
...
09:10:46.583 INFO  - Sensor CxxSquidSensor
09:10:46.589 DEBUG - setIncludeDirectories() adding dir 'inc1'
09:10:46.589 DEBUG - setIncludeDirectories() adding dir 'inc2'
09:10:46.590 DEBUG - setIncludeDirectories() adding dir 'inc3'
09:10:46.644 DEBUG - storing include root: '/var/tmp/src/inc1'
09:10:46.645 DEBUG - storing include root: '/var/tmp/src/inc3'
09:10:46.645 DEBUG - storing include root: '/var/tmp/src/inc2'
09:10:46.704 DEBUG - [/var/tmp/src/x.c:1]: processing #include "x.h", resolved to file '/var/tmp/src/inc3/x.h'
09:10:46.720 DEBUG - finished preprocessing '/var/tmp/src/x.c'
...
```

This patch corrects the problem of the include directories being searched in
the wrong order (as the above example shows inc3 being searched before inc2).

Running sonar-runner WITH this patch yields the following output:
```
09:21:33.507 INFO  - Sensor CxxSquidSensor
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc1'
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc2'
09:21:33.510 DEBUG - setIncludeDirectories() adding dir 'inc3'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc1'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc2'
09:21:33.553 DEBUG - storing include root: '/var/tmp/src/inc3'
09:21:33.603 DEBUG - [/var/tmp/src/x.c:1]: processing #include "x.h", resolved to file '/var/tmp/src/inc2/x.h'
```

See also SonarOpenCommunity#745.
@guwirth
Copy link
Collaborator

guwirth commented Jan 7, 2016

Looks good. If impact on performance is not too high I would merge it.
👍

guwirth added a commit that referenced this pull request Jan 8, 2016
correct search order of dirs specified via 'sonar.cxx.includeDirectories'
@guwirth guwirth merged commit 6e61c7a into SonarOpenCommunity:master Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants