Skip to content

Commit 75265f7

Browse files
authored
Merge pull request #8947 from redsun82/swift-pragma-once
Swift: use `#pragma once`
2 parents 2374e6b + c4fae08 commit 75265f7

File tree

10 files changed

+14
-41
lines changed

10 files changed

+14
-41
lines changed

swift/codegen/lib/render.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self):
2222
self._r = pystache.Renderer(search_dirs=str(paths.templates_dir), escape=lambda u: u)
2323
self.written = set()
2424

25-
def render(self, data, output: pathlib.Path, guard_base: pathlib.Path = None):
25+
def render(self, data, output: pathlib.Path):
2626
""" Render `data` to `output`.
2727
2828
`data` must have a `template` attribute denoting which template to use from the template directory.
@@ -34,10 +34,7 @@ def render(self, data, output: pathlib.Path, guard_base: pathlib.Path = None):
3434
"""
3535
mnemonic = type(data).__name__
3636
output.parent.mkdir(parents=True, exist_ok=True)
37-
guard = None
38-
if guard_base is not None:
39-
guard = str(output.relative_to(guard_base)).replace("/", "_").replace(".", "_").upper()
40-
data = self._r.render_name(data.template, data, generator=paths.exe_file, guard=guard)
37+
data = self._r.render_name(data.template, data, generator=paths.exe_file)
4138
with open(output, "w") as out:
4239
out.write(data)
4340
log.debug(f"generated {mnemonic} {output.name}")
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// generated by {{generator}}
22
// clang-format off
3-
#ifndef SWIFT_EXTRACTOR_TRAP_{{guard}}
4-
#define SWIFT_EXTRACTOR_TRAP_{{guard}}
3+
#pragma once
54

65
namespace codeql {
76
{{#tags}}
@@ -12,4 +11,3 @@ struct {{name}}Tag {{#has_bases}}: {{#bases}}{{^first}}, {{/first}}{{base}}Tag{{
1211
};
1312
{{/tags}}
1413
}
15-
#endif

swift/codegen/templates/cpp_traps.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// generated by {{generator}}
22
// clang-format off
3-
#ifndef SWIFT_EXTRACTOR_TRAP_{{guard}}
4-
#define SWIFT_EXTRACTOR_TRAP_{{guard}}
3+
#pragma once
54

65
#include <iostream>
76
#include <string>
@@ -32,4 +31,3 @@ inline std::ostream &operator<<(std::ostream &out, const {{name}}Trap &e) {
3231
{{/traps}}
3332

3433
}
35-
#endif

swift/codegen/test/test_ql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,16 @@ def test_class_db_id():
8585
cls = ql.Class("ThisIsMyClass")
8686
assert cls.db_id == "@this_is_my_class"
8787

88+
8889
def test_root_class():
8990
cls = ql.Class("Class")
9091
assert cls.root
9192

93+
9294
def test_non_root_class():
9395
cls = ql.Class("Class", bases=["A"])
9496
assert not cls.root
9597

98+
9699
if __name__ == '__main__':
97100
sys.exit(pytest.main())

swift/codegen/test/test_render.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import pathlib
32
from unittest import mock
43

54
import pytest
@@ -41,7 +40,7 @@ def test_render(pystache_renderer, sut):
4140
with mock.patch("builtins.open", mock.mock_open()) as output_stream:
4241
sut.render(data, output)
4342
assert pystache_renderer.mock_calls == [
44-
mock.call.render_name(data.template, data, generator=paths.exe_file, guard=None),
43+
mock.call.render_name(data.template, data, generator=paths.exe_file),
4544
]
4645
assert output_stream.mock_calls == [
4746
mock.call(output, 'w'),
@@ -52,17 +51,6 @@ def test_render(pystache_renderer, sut):
5251
assert sut.written == {output}
5352

5453

55-
def test_render_with_guard(pystache_renderer, sut):
56-
guard_base = pathlib.Path("test", "guard")
57-
data = mock.Mock()
58-
output = guard_base / "this" / "is" / "a" / "header.h"
59-
with mock.patch("builtins.open", mock.mock_open()) as output_stream:
60-
sut.render(data, output, guard_base=guard_base)
61-
assert pystache_renderer.mock_calls == [
62-
mock.call.render_name(data.template, data, generator=paths.exe_file, guard="THIS_IS_A_HEADER_H"),
63-
]
64-
65-
6654
def test_written(sut):
6755
data = [mock.Mock() for _ in range(4)]
6856
output = [mock.Mock() for _ in data]

swift/codegen/trapgen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def generate(opts, renderer):
117117
tag_graph[e.lhs]["derived"].append(d.type)
118118
tag_graph[d.type]["bases"].append(e.lhs)
119119

120-
renderer.render(cpp.TrapList(traps), out / "TrapEntries.h", guard_base=out)
120+
renderer.render(cpp.TrapList(traps), out / "TrapEntries.h")
121121

122122
tags = []
123123
for index, tag in enumerate(get_topologically_ordered_tags(tag_graph)):
@@ -127,7 +127,7 @@ def generate(opts, renderer):
127127
index=index,
128128
id=tag,
129129
))
130-
renderer.render(cpp.TagList(tags), out / "TrapTags.h", guard_base=out)
130+
renderer.render(cpp.TagList(tags), out / "TrapTags.h")
131131

132132

133133
tags = ("trap", "dbscheme")

swift/extractor/SwiftExtractor.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef SWIFT_EXTRACTOR_H_
2-
#define SWIFT_EXTRACTOR_H_
1+
#pragma once
32

43
#include "SwiftExtractorConfiguration.h"
54
#include <swift/AST/SourceFile.h>
@@ -10,5 +9,3 @@ namespace codeql {
109
void extractSwiftFiles(const SwiftExtractorConfiguration& config,
1110
swift::CompilerInstance& compiler);
1211
} // namespace codeql
13-
14-
#endif // SWIFT_EXTRACTOR_H_

swift/extractor/SwiftExtractorConfiguration.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef SWIFT_EXTRACTOR_CONFIGURATION_H_
2-
#define SWIFT_EXTRACTOR_CONFIGURATION_H_
1+
#pragma once
32

43
#include <string>
54
#include <vector>
@@ -14,5 +13,3 @@ struct SwiftExtractorConfiguration {
1413
std::vector<std::string> frontendOptions;
1514
};
1615
} // namespace codeql
17-
18-
#endif // SWIFT_EXTRACTOR_CONFIGURATION_H_

swift/extractor/trap/TrapLabel.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef SWIFT_EXTRACTOR_TRAP_LABEL_H
2-
#define SWIFT_EXTRACTOR_TRAP_LABEL_H
1+
#pragma once
32

43
#include <iomanip>
54
#include <iostream>
@@ -65,4 +64,3 @@ struct hash<codeql::UntypedTrapLabel> {
6564
}
6665
};
6766
} // namespace std
68-
#endif // SWIFT_EXTRACTOR_LIB_EXTRACTOR_CPP_TRAP_LABEL_H_

swift/extractor/trap/TrapTagTraits.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef SWIFT_EXTRACTOR_INCLUDE_EXTRACTOR_TRAP_TAGTRAITS_H
2-
#define SWIFT_EXTRACTOR_INCLUDE_EXTRACTOR_TRAP_TAGTRAITS_H
1+
#pragma once
32

43
#include <type_traits>
54

@@ -14,5 +13,3 @@ template <typename T>
1413
using ToTag = typename ToTagOverride<std::remove_const_t<T>>::type;
1514

1615
} // namespace codeql::trap
17-
18-
#endif // SWIFT_EXTRACTOR_INCLUDE_EXTRACTOR_TRAP_TAGTRAITS_H

0 commit comments

Comments
 (0)