Lint against named asm labels
This commit is contained in:
parent
7f3dc04644
commit
75915ad16f
6 changed files with 506 additions and 6 deletions
|
@ -7,7 +7,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
|
|||
use rustc_expand::base::{self, *};
|
||||
use rustc_parse::parser::Parser;
|
||||
use rustc_parse_format as parse;
|
||||
use rustc_session::lint;
|
||||
use rustc_session::lint::{self, BuiltinLintDiagnostics};
|
||||
use rustc_span::symbol::Ident;
|
||||
use rustc_span::symbol::{kw, sym, Symbol};
|
||||
use rustc_span::{InnerSpan, Span};
|
||||
|
@ -397,7 +397,11 @@ fn parse_reg<'a>(
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::InlineAsm> {
|
||||
fn expand_preparsed_asm(
|
||||
ecx: &mut ExtCtxt<'_>,
|
||||
args: AsmArgs,
|
||||
is_local_asm: bool,
|
||||
) -> Option<ast::InlineAsm> {
|
||||
let mut template = vec![];
|
||||
// Register operands are implicitly used since they are not allowed to be
|
||||
// referenced in the template string.
|
||||
|
@ -469,6 +473,70 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
|
|||
}
|
||||
}
|
||||
|
||||
if is_local_asm {
|
||||
let find_label_span = |needle: &str| -> Option<Span> {
|
||||
if let Some(snippet) = &template_snippet {
|
||||
if let Some(pos) = snippet.find(needle) {
|
||||
let end = pos
|
||||
+ &snippet[pos..]
|
||||
.find(|c| c == ':')
|
||||
.unwrap_or(snippet[pos..].len() - 1);
|
||||
let inner = InnerSpan::new(pos, end);
|
||||
return Some(template_sp.from_inner(inner));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
};
|
||||
|
||||
let mut found_labels = Vec::new();
|
||||
|
||||
// A semicolon might not actually be specified as a separator for all targets, but it seems like LLVM accepts it always
|
||||
let statements = template_str.split(|c| matches!(c, '\n' | ';'));
|
||||
for statement in statements {
|
||||
let mut start_idx = 0;
|
||||
for (idx, _) in statement.match_indices(':') {
|
||||
let possible_label = statement[start_idx..idx].trim();
|
||||
let mut chars = possible_label.chars();
|
||||
if let Some(c) = chars.next() {
|
||||
// A label starts with an alphabetic character and continues with alphanumeric characters
|
||||
if c.is_alphabetic() {
|
||||
if chars.all(char::is_alphanumeric) {
|
||||
found_labels.push(possible_label);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
start_idx = idx + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if found_labels.len() > 0 {
|
||||
let spans =
|
||||
found_labels.into_iter().filter_map(find_label_span).collect::<Vec<Span>>();
|
||||
if spans.len() > 0 {
|
||||
for span in spans.into_iter() {
|
||||
ecx.parse_sess().buffer_lint_with_diagnostic(
|
||||
lint::builtin::NAMED_ASM_LABELS,
|
||||
span,
|
||||
ecx.current_expansion.lint_node_id,
|
||||
"do not use named labels in inline assembly",
|
||||
BuiltinLintDiagnostics::NamedAsmLabel("Only GAS local labels of the form `N:` where N is a number may be used in inline asm".to_string()),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// If there were labels but we couldn't find a span, combine the warnings and use the template span
|
||||
ecx.parse_sess().buffer_lint_with_diagnostic(
|
||||
lint::builtin::NAMED_ASM_LABELS,
|
||||
template_span,
|
||||
ecx.current_expansion.lint_node_id,
|
||||
"do not use named labels in inline assembly",
|
||||
BuiltinLintDiagnostics::NamedAsmLabel("Only GAS local labels of the form `N:` where N is a number may be used in inline asm".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't treat raw asm as a format string.
|
||||
if args.options.contains(ast::InlineAsmOptions::RAW) {
|
||||
template.push(ast::InlineAsmTemplatePiece::String(template_str.to_string()));
|
||||
|
@ -670,7 +738,7 @@ pub fn expand_asm<'cx>(
|
|||
) -> Box<dyn base::MacResult + 'cx> {
|
||||
match parse_args(ecx, sp, tts, false) {
|
||||
Ok(args) => {
|
||||
let expr = if let Some(inline_asm) = expand_preparsed_asm(ecx, args) {
|
||||
let expr = if let Some(inline_asm) = expand_preparsed_asm(ecx, args, true) {
|
||||
P(ast::Expr {
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
|
||||
|
@ -697,7 +765,7 @@ pub fn expand_global_asm<'cx>(
|
|||
) -> Box<dyn base::MacResult + 'cx> {
|
||||
match parse_args(ecx, sp, tts, true) {
|
||||
Ok(args) => {
|
||||
if let Some(inline_asm) = expand_preparsed_asm(ecx, args) {
|
||||
if let Some(inline_asm) = expand_preparsed_asm(ecx, args, false) {
|
||||
MacEager::items(smallvec![P(ast::Item {
|
||||
ident: Ident::invalid(),
|
||||
attrs: Vec::new(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue