Skip to content

Commit 9be94f6

Browse files
committed
Provide mechanisms to inspect warning settings from outside lint.
1 parent 01b5777 commit 9be94f6

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

src/rustc/middle/lint.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import syntax::print::pprust::expr_to_str;
1111
export lint, ctypes, unused_imports;
1212
export level, ignore, warn, error;
1313
export lookup_lint, lint_dict, get_lint_dict, check_crate;
14-
export warning_settings;
14+
export warning_settings, warning_methods;
1515

1616
#[doc="
1717
@@ -21,6 +21,14 @@ basis. They contrast with static constraints enforced by other phases of the
2121
compiler, which are generally required to hold in order to compile the program
2222
at all.
2323
24+
We also build up a table containing information about lint settings, in order
25+
to allow other passes to take advantage of the warning attribute
26+
infrastructure. To save space, the table is keyed by the id of /items/, not of
27+
every expression. When an item has the default settings, the entry will be
28+
omitted. If we start allowing warn attributes on expressions, we will start
29+
having entries for expressions that do not share their enclosing items
30+
settings.
31+
2432
"]
2533

2634
enum lint {
@@ -100,6 +108,39 @@ type warning_settings = {
100108
settings_map: lint_mode_map
101109
};
102110

111+
fn get_warning_level(modes: lint_modes, lint: lint) -> level {
112+
alt modes.find(lint as uint) {
113+
some(c) { c }
114+
none { ignore }
115+
}
116+
}
117+
118+
fn span_lint(tcx: ty::ctxt, level: level, span: span, msg: str) {
119+
alt level {
120+
ignore { }
121+
warn { tcx.sess.span_warn(span, msg); }
122+
error { tcx.sess.span_err(span, msg); }
123+
}
124+
}
125+
126+
impl warning_methods for warning_settings {
127+
fn get_level(lint_mode: lint,
128+
_expr_id: ast::node_id, item_id: ast::node_id) -> level {
129+
alt self.settings_map.find(item_id) {
130+
some(modes) { get_warning_level(modes, lint_mode) }
131+
none { get_warning_level(self.default_settings, lint_mode) }
132+
}
133+
}
134+
135+
fn span_lint(tcx: ty::ctxt, lint_mode: lint,
136+
expr_id: ast::node_id, item_id: ast::node_id,
137+
span: span, msg: str) {
138+
let level = self.get_level(lint_mode, expr_id, item_id);
139+
span_lint(tcx, level, span, msg);
140+
}
141+
142+
}
143+
103144
// This is kind of unfortunate. It should be somewhere else, or we should use
104145
// a persistent data structure...
105146
fn clone_lint_modes(modes: lint_modes) -> lint_modes {
@@ -115,10 +156,7 @@ type ctxt = {dict: lint_dict,
115156

116157
impl methods for ctxt {
117158
fn get_level(lint: lint) -> level {
118-
alt self.curr.find(lint as uint) {
119-
some(c) { c }
120-
none { ignore }
121-
}
159+
get_warning_level(self.curr, lint)
122160
}
123161

124162
fn set_level(lint: lint, level: level) {
@@ -130,11 +168,7 @@ impl methods for ctxt {
130168
}
131169

132170
fn span_lint(level: level, span: span, msg: str) {
133-
alt level {
134-
ignore { }
135-
warn { self.tcx.sess.span_warn(span, msg); }
136-
error { self.tcx.sess.span_err(span, msg); }
137-
}
171+
span_lint(self.tcx, level, span, msg);
138172
}
139173

140174
#[doc="

0 commit comments

Comments
 (0)