1
+ // Copyright (c) 2025 Roberto Raggi <[email protected] >
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ // SOFTWARE.
20
+
21
+ #include " check_expression_types.h"
22
+
23
+ #include < cxx/ast.h>
24
+ #include < cxx/ast_visitor.h>
25
+
26
+ #include < format>
27
+
28
+ namespace cxx {
29
+
30
+ namespace {
31
+
32
+ class CheckExpressionTypes final : private ASTVisitor {
33
+ public:
34
+ [[nodiscard]] auto operator ()(TranslationUnit* unit) {
35
+ std::size_t missingTypes = 0 ;
36
+ std::swap (unit_, unit);
37
+ std::swap (missingTypes_, missingTypes);
38
+
39
+ accept (unit_->ast ());
40
+
41
+ std::swap (unit_, unit);
42
+ std::swap (missingTypes_, missingTypes);
43
+
44
+ return missingTypes == 0 ;
45
+ }
46
+
47
+ private:
48
+ using ASTVisitor::visit;
49
+
50
+ auto preVisit (AST* ast) -> bool override {
51
+ if (ast_cast<TemplateDeclarationAST>(ast)) {
52
+ // skip template declarations, as they are not instantiated yet
53
+ return false ;
54
+ }
55
+
56
+ if (auto expression = ast_cast<ExpressionAST>(ast)) {
57
+ if (!expression->type ) {
58
+ const auto loc = expression->firstSourceLocation ();
59
+
60
+ unit_->warning (loc, std::format (" untyped expression of kind '{}'" ,
61
+ to_string (expression->kind ())));
62
+
63
+ ++missingTypes_;
64
+ return false ;
65
+ }
66
+ }
67
+
68
+ return true ; // visit children
69
+ }
70
+
71
+ private:
72
+ TranslationUnit* unit_ = nullptr ;
73
+ std::size_t missingTypes_ = 0 ;
74
+ };
75
+
76
+ } // namespace
77
+
78
+ auto checkExpressionTypes (TranslationUnit& unit) -> bool {
79
+ CheckExpressionTypes checkExpressionTypes;
80
+ return checkExpressionTypes (&unit);
81
+ }
82
+
83
+ } // namespace cxx
0 commit comments