Skip to content

Warn on align directive with non-zero fill value in virtual sections #66792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion llvm/lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,7 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
bool HasFillExpr = false;
int64_t FillExpr = 0;
int64_t MaxBytesToFill = 0;
SMLoc FillExprLoc;

auto parseAlign = [&]() -> bool {
if (parseAbsoluteExpression(Alignment))
Expand All @@ -3402,7 +3403,7 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
// .align 3,,4
if (getTok().isNot(AsmToken::Comma)) {
HasFillExpr = true;
if (parseAbsoluteExpression(FillExpr))
if (parseTokenLoc(FillExprLoc) || parseAbsoluteExpression(FillExpr))
return true;
}
if (parseOptionalToken(AsmToken::Comma))
Expand Down Expand Up @@ -3451,6 +3452,17 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
}
}

if (HasFillExpr) {
MCSection *Sec = getStreamer().getCurrentSectionOnly();
if (Sec && Sec->isVirtualSection()) {
ReturnVal |=
Warning(FillExprLoc, "ignoring non-zero fill value in " +
Sec->getVirtualSectionKind() + " section '" +
Sec->getName() + "'");
FillExpr = 0;
}
}

// Diagnose non-sensical max bytes to align.
if (MaxBytesLoc.isValid()) {
if (MaxBytesToFill < 1) {
Expand Down
3 changes: 3 additions & 0 deletions llvm/test/MC/ELF/nobits-non-zero-value.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
# CHECK: {{.*}}.s:[[#@LINE+1]]:3: error: SHT_NOBITS section '.bss' cannot have instructions
addb %al,(%rax)

# CHECK: {{.*}}.s:[[#@LINE+1]]:11: warning: ignoring non-zero fill value in SHT_NOBITS section '.bss'
.align 4, 42

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check lines for --no-warn / --fatal-warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check lines for --no-warn / --fatal-warnings?

Can you walk me through why we might want that, please? I'd have guessed that if we properly emit a warning and the diagnostic infrastructure is working as intended then we wouldn't need to test such interactions for all kinds of warnings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning() returns true (indicating an error) only in presense of --fatal-warnings. We might want to add the --fatal-warnings check to test that we didn't forget ReturnVal |= part. (However, it appears that omitting it does not change the behavior because top-level loop not only checks for the return value, but also for what hasPendingErrors() returns.)
Regarding --no-warn, I tend to agree with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a RUN with --fatal-warnings. Let me know if it addresses your concerns properly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check line would match if Warning was replaced with Error. I expected something like:

# RUN: not llvm-mc ... | FileCheck %s
# RUN: not llvm-mc --fatal-warnings ... | FileCheck --check-prefix=FATAL %s
...
# CHECK: ... warning: ignoring ...
# FATAL: ... error: ignoring...

I grepped mc tests and found only 4 tests that excercise --fatal-warnings, so I guess you can just drop the last commit and go ahead...

# CHECK: <unknown>:0: error: SHT_NOBITS section '.bss' cannot have non-zero initializers
.long 1