Skip to content

Commit f33b24c

Browse files
authored
Merge pull request #1239 from jbj/qlformat-1
C++: Autoformat QL code in Architecture and Best Practices
2 parents f36eafc + 29aa5f5 commit f33b24c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+743
-518
lines changed

cpp/ql/src/Architecture/FeatureEnvy.ql

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
* statistical
1111
* non-attributable
1212
*/
13+
1314
import cpp
1415

1516
predicate functionUsesVariable(Function source, Variable v, File target) {
1617
v.getAnAccess().getEnclosingFunction() = source and
17-
not (v.(LocalScopeVariable).getFunction() = source) and
18+
not v.(LocalScopeVariable).getFunction() = source and
1819
v.getFile() = target
1920
}
2021

@@ -25,23 +26,23 @@ predicate functionUsesFunction(Function source, Function f, File target) {
2526

2627
predicate dependencyCount(Function source, File target, int res) {
2728
res = strictcount(Declaration d |
28-
functionUsesVariable(source, d, target) or
29-
functionUsesFunction(source, d, target)
30-
)
29+
functionUsesVariable(source, d, target) or
30+
functionUsesFunction(source, d, target)
31+
)
3132
}
3233

3334
predicate selfDependencyCountOrZero(Function source, int res) {
34-
exists(File target
35-
| target = source.getFile() and onlyInFile(source, target)
36-
| res = max(int i | dependencyCount(source, target, i) or i = 0))
35+
exists(File target | target = source.getFile() and onlyInFile(source, target) |
36+
res = max(int i | dependencyCount(source, target, i) or i = 0)
37+
)
3738
}
3839

3940
predicate dependsHighlyOn(Function source, File target, int res) {
4041
dependencyCount(source, target, res) and
4142
target.fromSource() and
4243
exists(int selfCount |
4344
selfDependencyCountOrZero(source, selfCount) and
44-
res > 2*selfCount and
45+
res > 2 * selfCount and
4546
res > 4
4647
)
4748
}
@@ -52,14 +53,18 @@ predicate onlyInFile(Function f, File file) {
5253
}
5354

5455
from Function f, File other, int selfCount, int depCount, string selfDeps
55-
where dependsHighlyOn(f, other, depCount) and
56-
selfDependencyCountOrZero(f, selfCount) and
57-
not exists(File yetAnother | dependsHighlyOn(f, yetAnother, _) and yetAnother != other) and
58-
not other instanceof HeaderFile and
59-
not f instanceof MemberFunction
60-
and if selfCount = 0 then selfDeps = "0 dependencies"
61-
else if selfCount = 1 then selfDeps = "only 1 dependency"
62-
else selfDeps = "only " + selfCount.toString() + " dependencies"
63-
select f, "Function " + f.getName() + " could be moved to file $@" +
64-
" since it has " + depCount.toString() + " dependencies to that file, but " +
65-
selfDeps + " to its own file.", other, other.getBaseName()
56+
where
57+
dependsHighlyOn(f, other, depCount) and
58+
selfDependencyCountOrZero(f, selfCount) and
59+
not exists(File yetAnother | dependsHighlyOn(f, yetAnother, _) and yetAnother != other) and
60+
not other instanceof HeaderFile and
61+
not f instanceof MemberFunction and
62+
if selfCount = 0
63+
then selfDeps = "0 dependencies"
64+
else
65+
if selfCount = 1
66+
then selfDeps = "only 1 dependency"
67+
else selfDeps = "only " + selfCount.toString() + " dependencies"
68+
select f,
69+
"Function " + f.getName() + " could be moved to file $@" + " since it has " + depCount.toString() +
70+
" dependencies to that file, but " + selfDeps + " to its own file.", other, other.getBaseName()

cpp/ql/src/Architecture/General Class-Level Information/ClassHierarchies.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @result_ondemand succeed 48
1010
* @tags maintainability
1111
*/
12+
1213
import cpp
1314

1415
from Class s

cpp/ql/src/Architecture/General Class-Level Information/HubClasses.ql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
* @treemap.warnOn highValues
88
* @tags maintainability
99
*/
10+
1011
import cpp
1112

1213
from Class c
1314
where c.fromSource()
14-
select c as Class,
15-
c.getMetrics().getAfferentCoupling() as AfferentCoupling,
16-
c.getMetrics().getEfferentSourceCoupling() as EfferentCoupling
17-
order by AfferentCoupling desc
15+
select c as Class, c.getMetrics().getAfferentCoupling() as AfferentCoupling,
16+
c.getMetrics().getEfferentSourceCoupling() as EfferentCoupling order by AfferentCoupling desc

cpp/ql/src/Architecture/General Class-Level Information/InheritanceDepthDistribution.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @result_ondemand succeed 48
1010
* @tags maintainability
1111
*/
12+
1213
import cpp
1314

1415
/** does source class c have inheritance depth d? */
@@ -18,6 +19,5 @@ predicate hasInheritanceDepth(Class c, int d) {
1819

1920
from int depth
2021
where hasInheritanceDepth(_, depth)
21-
select depth as InheritanceDepth,
22-
count(Class c | hasInheritanceDepth(c, depth)) as NumberOfClasses
23-
order by InheritanceDepth
22+
select depth as InheritanceDepth, count(Class c | hasInheritanceDepth(c, depth)) as NumberOfClasses
23+
order by InheritanceDepth

cpp/ql/src/Architecture/General Namespace-Level Information/CyclicNamespaces.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @tags maintainability
88
* modularity
99
*/
10+
1011
import cpp
1112

1213
from MetricNamespace a, MetricNamespace b

cpp/ql/src/Architecture/General Namespace-Level Information/GlobalNamespaceClasses.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* @tags maintainability
77
* modularity
88
*/
9+
910
import cpp
1011

1112
from Class c
12-
where c.fromSource()
13-
and c.isTopLevel()
14-
and c.getParentScope() instanceof GlobalNamespace
13+
where
14+
c.fromSource() and
15+
c.isTopLevel() and
16+
c.getParentScope() instanceof GlobalNamespace
1517
select c, "This class is not declared in any namespace"

cpp/ql/src/Architecture/General Namespace-Level Information/NamespaceDependencies.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @tags maintainability
88
* modularity
99
*/
10+
1011
import cpp
1112

1213
from MetricNamespace a, MetricNamespace b

cpp/ql/src/Architecture/General Top-Level Information/GeneralStatistics.ql

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,48 @@
55
* @id cpp/architecture/general-statistics
66
* @tags maintainability
77
*/
8+
89
import cpp
910

1011
from string l, string n
11-
where (l = "Number of Namespaces" and
12-
n = count(Namespace p | p.fromSource()).toString())
13-
or (l = "Number of Files" and
14-
n = count(File f | f.fromSource()).toString())
15-
or (l = "Number of Header Files" and
16-
n = count(HeaderFile f | f.fromSource()).toString())
17-
or (l = "Number of C Files" and
18-
n = count(CFile f | f.fromSource()).toString())
19-
or (l = "Number of C++ Files" and
20-
n = count(CppFile f | f.fromSource()).toString())
21-
or (l = "Number of Classes" and
22-
n = count(Class c | c.fromSource() and not c instanceof Struct).toString())
23-
or (l = "Number of Structs" and
24-
n = count(Struct s | s.fromSource()and not s instanceof Union).toString())
25-
or (l = "Number of Unions" and
26-
n = count(Union u | u.fromSource()).toString())
27-
or (l = "Number of Functions" and
28-
n = count(Function f | f.fromSource()).toString())
29-
or (l = "Number of Lines Of Code" and
30-
n = sum(File f, int toSum | (f.fromSource()) and (toSum = f.getMetrics().getNumberOfLinesOfCode()) | toSum).toString())
31-
or (l = "Self-Containedness" and
32-
n = (100 * sum(Class c, int toSum | (c.fromSource()) and (toSum = c.getMetrics().getEfferentSourceCoupling()) | toSum)
33-
/ sum(Class c, int toSum | (c.fromSource()) and (toSum = c.getMetrics().getEfferentCoupling()) | toSum)).toString()
34-
+ "%")
12+
where
13+
l = "Number of Namespaces" and
14+
n = count(Namespace p | p.fromSource()).toString()
15+
or
16+
l = "Number of Files" and
17+
n = count(File f | f.fromSource()).toString()
18+
or
19+
l = "Number of Header Files" and
20+
n = count(HeaderFile f | f.fromSource()).toString()
21+
or
22+
l = "Number of C Files" and
23+
n = count(CFile f | f.fromSource()).toString()
24+
or
25+
l = "Number of C++ Files" and
26+
n = count(CppFile f | f.fromSource()).toString()
27+
or
28+
l = "Number of Classes" and
29+
n = count(Class c | c.fromSource() and not c instanceof Struct).toString()
30+
or
31+
l = "Number of Structs" and
32+
n = count(Struct s | s.fromSource() and not s instanceof Union).toString()
33+
or
34+
l = "Number of Unions" and
35+
n = count(Union u | u.fromSource()).toString()
36+
or
37+
l = "Number of Functions" and
38+
n = count(Function f | f.fromSource()).toString()
39+
or
40+
l = "Number of Lines Of Code" and
41+
n = sum(File f, int toSum |
42+
f.fromSource() and toSum = f.getMetrics().getNumberOfLinesOfCode()
43+
|
44+
toSum
45+
).toString()
46+
or
47+
l = "Self-Containedness" and
48+
n = (
49+
100 * sum(Class c | c.fromSource() | c.getMetrics().getEfferentSourceCoupling()) /
50+
sum(Class c | c.fromSource() | c.getMetrics().getEfferentCoupling())
51+
).toString() + "%"
3552
select l as Title, n as Value

cpp/ql/src/Architecture/InappropriateIntimacy.ql

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* statistical
1111
* non-attributable
1212
*/
13+
1314
import cpp
1415

1516
predicate remoteVarAccess(File source, File target, VariableAccess va) {
@@ -48,16 +49,19 @@ predicate highDependencyCount(File source, File target, int res) {
4849
variableDependencyCount(source, target, varCount) and
4950
functionDependencyCount(source, target, funCount) and
5051
res = varCount + funCount and
51-
res > 20)
52+
res > 20
53+
)
5254
}
5355

5456
from File a, File b, int ca, int cb
55-
where highDependencyCount(a, b, ca) and
56-
highDependencyCount(b, a, cb) and
57-
ca >= cb and
58-
a != b and
59-
not a instanceof HeaderFile and
60-
not b instanceof HeaderFile and
61-
b.getShortName().trim().length() > 0
62-
select a, "File is too closely tied to $@ (" + ca.toString() + " dependencies one way and " + cb.toString() + " the other).",
63-
b, b.getBaseName()
57+
where
58+
highDependencyCount(a, b, ca) and
59+
highDependencyCount(b, a, cb) and
60+
ca >= cb and
61+
a != b and
62+
not a instanceof HeaderFile and
63+
not b instanceof HeaderFile and
64+
b.getShortName().trim().length() > 0
65+
select a,
66+
"File is too closely tied to $@ (" + ca.toString() + " dependencies one way and " + cb.toString() +
67+
" the other).", b, b.getBaseName()

cpp/ql/src/Architecture/Refactoring Opportunities/ClassesWithManyDependencies.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* statistical
1212
* non-attributable
1313
*/
14+
1415
import cpp
1516

1617
from Class t, int n
17-
where t.fromSource() and
18-
n = t.getMetrics().getEfferentSourceCoupling() and
19-
n > 10
18+
where
19+
t.fromSource() and
20+
n = t.getMetrics().getEfferentSourceCoupling() and
21+
n > 10
2022
select t as Class, "This class has too many dependencies (" + n.toString() + ")"

0 commit comments

Comments
 (0)