Skip to content

Commit cafdee7

Browse files
ci: add clang-tidy and clang-tidy-fix targets (#242)
* Add clang-tidy and clang-tidy-fix targets * Make linter target depend on starling-raw.wasm This is needed to build compile_command.json file that clang-tidy uses. * fix: clang-analyzer errors These are various fixes reported by clang-analyzer regarding memory leaks, unsafe functions and inconsistent use use of new and free. * fix: clang-analyzer errors in script loader This changes script loader paths to use owned string instead of raw pointers to fix potential memory leaks reported by clang. The tradeof is that now we call c_str in few places, but the impact on performance should be negligible since we call this only once. * fix: enable cppcoreguidelines checks and apply auto fixes This patch enables cppcoreguidelines but disables all the lints that don't offer auto fixes. * fix: enable modernize lints and apply auto fixes This patch enables modernize lints and disable all lints from this group that are not offering auto fix. * fix: use js_new for RefCounted types This fixes linter error, where clang complains about using new/free instead of new/delete. * fix: fix performance lint about unnecesairly large enum base * fix: enable performace checks and appy autofixes * fix: apply linter readability fixes * fix: do not use const members * fix: remove unused declarations * ci: add CI step for linter * fix: apply fixes after merging main * fix: enable no-malloc checks and apply fixes * fix: allow implicit pointer to bool conversions * Apply suggestions from code review Co-authored-by: Till Schneidereit <till@tillschneidereit.net> * fix: remove clang pragmas * fix: bounds constant array index * fix: enable special member functions lint * fix: enable type const cast check * fix: enable bugprone checks * fix: correct indentation * Apply suggestions from code review Co-authored-by: Till Schneidereit <till@tillschneidereit.net> * fix: align declaration arguments with impl --------- Co-authored-by: Till Schneidereit <till@tillschneidereit.net>
1 parent c0587e4 commit cafdee7

File tree

112 files changed

+2094
-1824
lines changed

Some content is hidden

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

112 files changed

+2094
-1824
lines changed

.clang-tidy

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Checks: >
2+
clang-analyzer-*,
3+
clang-diagnostic-*,
4+
cppcoreguidelines-*,
5+
modernize-*,
6+
performance-*,
7+
readability-*,
8+
bugprone-*,
9+
misc-redundant-expression,
10+
misc-uniqueptr-reset-release,
11+
misc-unused-alias-decls,
12+
misc-unused-using-decls,
13+
-clang-analyzer-optin.core.EnumCastOutOfRange,
14+
-cppcoreguidelines-avoid-do-while,
15+
-cppcoreguidelines-avoid-c-arrays,
16+
-cppcoreguidelines-pro-type-reinterpret-cast,
17+
-cppcoreguidelines-avoid-magic-numbers,
18+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
19+
-cppcoreguidelines-owning-memory,
20+
-cppcoreguidelines-avoid-non-const-global-variables,
21+
-cppcoreguidelines-pro-type-vararg,
22+
-cppcoreguidelines-narrowing-conversions,
23+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
24+
-cppcoreguidelines-pro-type-cstyle-cast,
25+
-cppcoreguidelines-init-variables,
26+
-cppcoreguidelines-macro-usage,
27+
-cppcoreguidelines-interfaces-global-init,
28+
-cppcoreguidelines-use-default-member-init,
29+
-cppcoreguidelines-pro-type-static-cast-downcast,
30+
-modernize-use-trailing-return-type,
31+
-modernize-avoid-c-arrays,
32+
-modernize-use-ranges,
33+
-readability-identifier-length,
34+
-readability-static-accessed-through-instance,
35+
-readability-function-cognitive-complexity,
36+
-readability-magic-numbers,
37+
-readability-simplify-boolean-expr,
38+
-bugprone-easily-swappable-parameters,
39+
-bugprone-dynamic-static-initializers,
40+
-bugprone-assignment-in-if-condition,
41+
-bugprone-narrowing-conversions,
42+
43+
# Check options configuration
44+
CheckOptions:
45+
- key: readability-implicit-bool-conversion.AllowPointerConditions
46+
value: 'true'
47+
48+
# Treat all warnings as errors
49+
WarningsAsErrors: '*'
50+
51+
# Only warn on headers in the code tree
52+
HeaderFilterRegex: 'builtins/|runtime/'
53+
54+
# Use the same style as clang-format (look for .clang-format)
55+
FormatStyle: file
56+
57+
# Explicitly exclude system/dependency headers
58+
SystemHeaders: false

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ jobs:
113113
name: spidermonkey-${{ matrix.build }}
114114
path: spidermonkey-dist-${{ matrix.build }}/*
115115

116+
- name: Install Just
117+
uses: taiki-e/install-action@just
118+
119+
- name: Run clang linter
120+
if: matrix.build == 'debug'
121+
run: just lint
122+
116123
release-spidermonkey:
117124
needs: test
118125
if: needs.test.outputs.SM_TAG_EXISTS == 'false' && (github.event_name == 'push' &&

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ include("spidermonkey")
4242
include("openssl")
4343
include("${HOST_API}/host_api.cmake")
4444
include("build-crates")
45+
include("lint")
4546

4647
option(ENABLE_JS_DEBUGGER "Enable support for debugging content via a socket connection" ON)
4748

builtins/web/abort/abort-controller.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "abort-controller.h"
22
#include "abort-signal.h"
33

4-
namespace builtins {
5-
namespace web {
6-
namespace abort {
4+
5+
6+
namespace builtins::web::abort {
77

88
const JSFunctionSpec AbortController::static_methods[] = {
99
JS_FS_END,
@@ -65,6 +65,6 @@ bool AbortController::init_class(JSContext *cx, JS::HandleObject global) {
6565
return init_class_impl(cx, global);
6666
}
6767

68-
} // namespace abort
69-
} // namespace web
70-
} // namespace builtins
68+
} // namespace builtins::web::abort
69+
70+

builtins/web/abort/abort-controller.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#include "builtin.h"
55

6-
namespace builtins {
7-
namespace web {
8-
namespace abort {
6+
7+
8+
namespace builtins::web::abort {
99

1010
class AbortController : public BuiltinImpl<AbortController> {
1111
static bool signal_get(JSContext *cx, unsigned argc, JS::Value *vp);
@@ -17,7 +17,7 @@ class AbortController : public BuiltinImpl<AbortController> {
1717
static constexpr const char *class_name = "AbortController";
1818
static constexpr unsigned ctor_length = 0;
1919

20-
enum Slots { Signal = 0, Count };
20+
enum Slots : uint8_t { Signal = 0, Count };
2121

2222
static const JSFunctionSpec static_methods[];
2323
static const JSPropertySpec static_properties[];
@@ -28,9 +28,9 @@ class AbortController : public BuiltinImpl<AbortController> {
2828
static bool constructor(JSContext *cx, unsigned argc, Value *vp);
2929
};
3030

31-
} // namespace abort
32-
} // namespace web
33-
} // namespace builtins
31+
} // namespace builtins::web::abort
32+
33+
3434

3535

3636

builtins/web/abort/abort-signal.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include "../event/event.h"
66
#include "../timers.h"
77

8-
namespace builtins {
9-
namespace web {
10-
namespace abort {
8+
9+
10+
namespace builtins::web::abort {
1111

1212
using event::Event;
1313
using event::EventTarget;
@@ -195,7 +195,7 @@ bool AbortSignal::add_algorithm(JSObject *self, js::UniquePtr<AbortAlgorithm> al
195195
}
196196

197197
// 2. Append algorithm to signal's abort algorithms.
198-
auto algorithms = AbortSignal::algorithms(self);
198+
auto *algorithms = AbortSignal::algorithms(self);
199199
return algorithms->append(std::move(algorithm));
200200
}
201201

@@ -228,7 +228,7 @@ bool AbortSignal::abort(JSContext *cx, HandleObject self, HandleValue reason) {
228228
JS::RootedObjectVector dep_signals_to_abort(cx);
229229

230230
// 4. For each dependentSignal of signal's dependent signals:
231-
auto dep_signals = dependent_signals(self);
231+
auto *dep_signals = dependent_signals(self);
232232
for (auto const &sig : dep_signals->items()) {
233233
RootedObject signal(cx, sig);
234234
// 1. If dependentSignal is not aborted:
@@ -259,9 +259,11 @@ bool AbortSignal::abort(JSContext *cx, HandleObject self, HandleValue reason) {
259259
bool AbortSignal::run_abort_steps(JSContext *cx, HandleObject self) {
260260
// To run the abort steps for an AbortSignal signal:
261261
// 1. For each algorithm of signal's abort algorithms: run algorithm.
262-
auto algorithms = AbortSignal::algorithms(self);
262+
auto *algorithms = AbortSignal::algorithms(self);
263263
for (auto &algorithm : *algorithms) {
264-
if (!algorithm->run(cx)) return false;
264+
if (!algorithm->run(cx)) {
265+
return false;
266+
}
265267
}
266268

267269
// 2. Empty signals's abort algorithms.
@@ -274,11 +276,7 @@ bool AbortSignal::run_abort_steps(JSContext *cx, HandleObject self) {
274276
RootedObject event(cx, Event::create(cx, type_val, JS::NullHandleValue));
275277
RootedValue event_val(cx, JS::ObjectValue(*event));
276278

277-
if (!EventTarget::dispatch_event(cx, self, event_val, &res_val)) {
278-
return false;
279-
}
280-
281-
return true;
279+
return EventTarget::dispatch_event(cx, self, event_val, &res_val);
282280
}
283281

284282
// Set signal's abort reason to reason if it is given; otherwise to a new "AbortError" DOMException.
@@ -420,7 +418,7 @@ JSObject *AbortSignal::create_with_signals(JSContext *cx, HandleValueArray signa
420418

421419
// 3. Set resultSignal's dependent to true.
422420
SetReservedSlot(self, Slots::Dependent, JS::TrueValue());
423-
auto our_signals = source_signals(self);
421+
auto *our_signals = source_signals(self);
424422

425423
// 4. For each signal of signals:
426424
for (size_t i = 0; i < signals.length(); ++i) {
@@ -431,20 +429,20 @@ JSObject *AbortSignal::create_with_signals(JSContext *cx, HandleValueArray signa
431429
// 1. Append signal to resultSignal's source signals.
432430
our_signals->insert(signal);
433431
// 2. Append resultSignal to signal's dependent signals.
434-
auto their_signals = dependent_signals(signal);
432+
auto *their_signals = dependent_signals(signal);
435433
their_signals->insert(self);
436434
}
437435
// 2. Otherwise...
438436
else {
439-
auto src_signals = source_signals(signal);
437+
auto *src_signals = source_signals(signal);
440438
// for each sourceSignal of signal's source signals:
441439
for (auto const &source : src_signals->items()) {
442440
// 1. Assert: sourceSignal is not aborted and not dependent.
443441
MOZ_ASSERT(!is_aborted(source) && !is_dependent(source));
444442
// 2. Append sourceSignal to resultSignal's source signals.
445443
our_signals->insert(source);
446444
// 3. Append resultSignal to sourceSignal's dependent signals.
447-
auto their_signals = dependent_signals(source);
445+
auto *their_signals = dependent_signals(source);
448446
their_signals->insert(self);
449447
};
450448
}
@@ -469,21 +467,21 @@ void AbortSignal::trace(JSTracer *trc, JSObject *self) {
469467

470468
auto has_sources = !JS::GetReservedSlot(self, Slots::SourceSignals).isNullOrUndefined();
471469
if (has_sources) {
472-
auto srcsig = source_signals(self);
470+
auto *srcsig = source_signals(self);
473471
srcsig->trace(trc);
474472
srcsig->traceWeak(trc);
475473
}
476474

477475
auto has_deps = !JS::GetReservedSlot(self, Slots::DependentSignals).isNullOrUndefined();
478476
if (has_deps) {
479-
auto depsig = dependent_signals(self);
477+
auto *depsig = dependent_signals(self);
480478
depsig->trace(trc);
481479
depsig->traceWeak(trc);
482480
}
483481

484482
auto has_algorithms = !JS::GetReservedSlot(self, Slots::Algorithms).isNullOrUndefined();
485483
if (has_algorithms) {
486-
auto algorithms = AbortSignal::algorithms(self);
484+
auto *algorithms = AbortSignal::algorithms(self);
487485
algorithms->trace(trc);
488486
}
489487
}
@@ -515,6 +513,6 @@ bool install(api::Engine *engine) {
515513

516514
JSString *AbortSignal::abort_type_atom = nullptr;
517515

518-
} // namespace abort
519-
} // namespace web
520-
} // namespace builtins
516+
} // namespace builtins::web::abort
517+
518+

builtins/web/abort/abort-signal.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66

77
#include "../event/event-target.h"
88

9-
namespace builtins {
10-
namespace web {
11-
namespace abort {
9+
10+
11+
namespace builtins::web::abort {
1212

1313
struct AbortAlgorithm {
1414
bool virtual run(JSContext *cx) = 0;
1515
void virtual trace(JSTracer *trc) { };
1616

17+
AbortAlgorithm() = default;
1718
virtual ~AbortAlgorithm() = default;
19+
20+
AbortAlgorithm(const AbortAlgorithm &) = default;
21+
AbortAlgorithm(AbortAlgorithm &&) = delete;
22+
23+
AbortAlgorithm &operator=(const AbortAlgorithm &) = default;
24+
AbortAlgorithm &operator=(AbortAlgorithm &&) = delete;
1825
};
1926

2027
class AbortSignal : public BuiltinImpl<AbortSignal, TraceableClassPolicy> {
@@ -45,7 +52,7 @@ class AbortSignal : public BuiltinImpl<AbortSignal, TraceableClassPolicy> {
4552

4653
public:
4754
static constexpr int ParentSlots = event::EventTarget::Slots::Count;
48-
enum Slots {
55+
enum Slots : uint8_t {
4956
Reason = ParentSlots,
5057
Algorithms,
5158
Dependent,
@@ -81,8 +88,8 @@ class AbortSignal : public BuiltinImpl<AbortSignal, TraceableClassPolicy> {
8188
static void trace(JSTracer *trc, JSObject *self);
8289
};
8390

84-
} // namespace abort
85-
} // namespace web
86-
} // namespace builtins
91+
} // namespace builtins::web::abort
92+
93+
8794

8895
#endif // BUILTINS_WEB_ABORT_SIGNAL_H_

builtins/web/abort/weak-index-set.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ class WeakIndexSet {
1313
WeakVec items_;
1414

1515
public:
16-
WeakIndexSet() : items_() {}
16+
WeakIndexSet() = default;
1717

1818
bool insert(JSObject* obj) {
19-
auto it = std::find_if(items_.begin(), items_.end(), [&obj](const auto &item) { return item == obj; });
19+
auto *it = std::find_if(items_.begin(), items_.end(), [&obj](const auto &item) { return item == obj; });
2020

2121
if (it == items_.end()) {
2222
return items_.append(obj);
23-
} else {
24-
return true;
2523
}
24+
return true;
2625
}
2726

2827
bool remove(JSObject* obj) {
@@ -31,7 +30,7 @@ class WeakIndexSet {
3130
}
3231

3332
WeakVec &items() { return items_;}
34-
const WeakVec &items() const { return items_; }
33+
[[nodiscard]] const WeakVec &items() const { return items_; }
3534

3635
void trace(JSTracer* trc) { items_.trace(trc); }
3736

0 commit comments

Comments
 (0)