GCC Code Coverage Report


Directory: ./
File: src/ast/ops/attribute.cpp
Date: 2023-04-27 00:55:30
Exec Total Coverage
Lines: 42 46 91.3%
Functions: 10 10 100.0%
Branches: 31 34 91.2%

Line Branch Exec Source
1 #include "ast/nodes.h"
2 #include "ast/ops.h"
3
4 namespace lython {
5
6 struct IsAttr {
7 using Return = StmtNode*;
8
9 IsAttr(String const& n): name(n) {}
10
11 String const& name;
12
13 static Return isattr(StmtNode* stmt, String const& name) { return IsAttr(name).lookup(stmt); }
14
15 bool match(ExprNode* n) {
16
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (n->kind == NodeKind::Name) {
17 20 auto nn = cast<Name>(n);
18
3/3
✓ Branch 1 taken 20 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 16 times.
20 if (nn->id == name) {
19 4 return true;
20 }
21 }
22 16 return false;
23 }
24
25 Return assign(Assign* n) {
26
2/2
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 7 times.
16 for (auto& target: n->targets) {
27
3/3
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 7 times.
9 if (match(target)) {
28 2 return n;
29 }
30 }
31 7 return nullptr;
32 }
33 Return annassign(AnnAssign* n) {
34
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9 times.
11 if (match(n->target))
35 2 return n;
36 9 return nullptr;
37 }
38
39 Return functiondef(FunctionDef* n) {
40
3/3
✓ Branch 1 taken 12 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 8 times.
12 if (n->name == name) {
41 4 return n;
42 }
43 8 return nullptr;
44 }
45
46 Return classdef(ClassDef* n) {
47
3/3
✓ Branch 1 taken 3 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
3 if (n->name == name) {
48 2 return n;
49 }
50 1 return nullptr;
51 }
52
53 Return lookup(StmtNode* obj) {
54
4/5
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
35 switch (obj->kind) {
55 12 case NodeKind::FunctionDef: {
56 12 auto n = cast<FunctionDef>(obj);
57 12 return functiondef(n);
58 }
59 9 case NodeKind::Assign: {
60 9 auto n = cast<Assign>(obj);
61 9 return assign(n);
62 }
63 11 case NodeKind::AnnAssign: {
64 11 auto n = cast<AnnAssign>(obj);
65 11 return annassign(n);
66 }
67 3 case NodeKind::ClassDef: {
68 3 auto n = cast<ClassDef>(obj);
69 3 return classdef(n);
70 }
71
72 default: break;
73 }
74 return nullptr;
75 }
76 };
77
78 StmtNode* getattr(StmtNode* obj, String const& attr, ExprNode*& type) {
79
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (obj->kind != NodeKind::ClassDef) {
80 1 return nullptr;
81 }
82
83 11 ClassDef* def = cast<ClassDef>(obj);
84
1/1
✓ Branch 1 taken 11 times.
11 int attrid = def->get_attribute(attr);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (attrid > -1) {
87 ClassDef::Attr& at = def->attributes[attrid];
88 return at.stmt;
89 }
90
91 // Static Lookup
92
2/2
✓ Branch 5 taken 35 times.
✓ Branch 6 taken 1 times.
36 for (auto& stmt: def->body) {
93
1/1
✓ Branch 1 taken 35 times.
35 auto value = IsAttr::isattr(stmt, attr);
94
95
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 25 times.
35 if (value != nullptr) {
96 10 return value;
97 }
98 }
99
100 1 return nullptr;
101 };
102
103 bool hasattr(StmtNode* obj, String const& attr) {
104 ExprNode* dummy;
105
1/1
✓ Branch 1 taken 7 times.
7 return getattr(obj, attr, dummy) != nullptr;
106 }
107
108 } // namespace lython
109