Debian Codegraph @

gcc/c-family/c-type-mismatch.cc

raw
1 /* Implementations of classes for reporting type mismatches.
2 Copyright (C) 2014-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "hash-set.h"
25 #include "vec.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "inchash.h"
30 #include "tree-core.h"
31 #include "tree.h"
32 #include "diagnostic-core.h"
33 #include "c-family/c-type-mismatch.h"
34 #include "print-tree.h"
35 #include "pretty-print.h"
36 #include "intl.h"
37 #include "cpplib.h"
38 #include "diagnostic.h"
39 #include "diagnostic-highlight-colors.h"
40
41 /* Implementation of range_label::get_text for
42 maybe_range_label_for_tree_type_mismatch.
43
44 If both expressions are non-NULL, then generate text describing
45 the first expression's type (using the other expression's type
46 for comparison, analogous to %H and %I in the C++ frontend, but
47 on expressions rather than types). */
48
49 label_text
50 maybe_range_label_for_tree_type_mismatch::get_text (unsigned range_idx) const
51 {
52 if (m_expr == NULL_TREE
53 || !EXPR_P (m_expr))
54 return label_text::borrow (NULL);
55 tree expr_type = TREE_TYPE (m_expr);
56
57 tree other_type = NULL_TREE;
58 if (m_other_expr && EXPR_P (m_other_expr))
59 other_type = TREE_TYPE (m_other_expr);
60
61 range_label_for_type_mismatch inner (expr_type, other_type);
62 return inner.get_text (range_idx);
63 }
64
65 /* binary_op_rich_location's ctor.
66
67 If use_operator_loc_p (LOC, ARG0, ARG1), then attempt to make a 3-location
68 rich_location of the form:
69
70 arg_0 op arg_1
71 ~~~~~ ^~ ~~~~~
72 | |
73 | arg1 type
74 arg0 type
75
76 labelling the types of the arguments if SHOW_TYPES is true,
77 and using highlight_colors::lhs and highlight_colors::rhs for the ranges.
78
79 Otherwise, make a 1-location rich_location using the compound
80 location within LOC:
81
82 arg_0 op arg_1
83 ~~~~~~^~~~~~~~
84
85 for which we can't label the types. */
86
87 binary_op_rich_location::binary_op_rich_location (const op_location_t &loc,
88 tree arg0, tree arg1,
89 bool show_types)
90 : gcc_rich_location (loc.m_combined_loc),
91 m_label_for_arg0 (arg0, arg1),
92 m_label_for_arg1 (arg1, arg0)
93 {
94 /* Default (above) to using the combined loc.
95 Potentially override it here: if we have location information for the
96 operator and for both arguments, then split them all out.
97 Alternatively, override it if we don't have the combined location. */
98 if (use_operator_loc_p (loc, arg0, arg1))
99 {
100 set_range (0, loc.m_operator_loc, SHOW_RANGE_WITH_CARET);
101 maybe_add_expr (arg0, show_types ? &m_label_for_arg0 : NULL,
102 highlight_colors::lhs);
103 maybe_add_expr (arg1, show_types ? &m_label_for_arg1 : NULL,
104 highlight_colors::rhs);
105 }
106 }
107
108 /* Determine if binary_op_rich_location's ctor should attempt to make
109 a 3-location rich_location (the location of the operator and of
110 the 2 arguments), or fall back to a 1-location rich_location showing
111 just the combined location of the operation as a whole. */
112
113 bool
114 binary_op_rich_location::use_operator_loc_p (const op_location_t &loc,
115 tree arg0, tree arg1)
116 {
117 /* If we don't have a combined location, then use the operator location,
118 and try to add ranges for the operators. */
119 if (loc.m_combined_loc == UNKNOWN_LOCATION)
120 return true;
121
122 /* If we don't have the operator location, then use the
123 combined location. */
124 if (loc.m_operator_loc == UNKNOWN_LOCATION)
125 return false;
126
127 /* We have both operator location and combined location: only use the
128 operator location if we have locations for both arguments. */
129 return (EXPR_HAS_LOCATION (arg0)
130 && EXPR_HAS_LOCATION (arg1));
131 }