================
@@ -469,11 +470,31 @@ Interpreter::Parse(llvm::StringRef Code) {
// printing could cause it.
getCompilerInstance()->getDiagnostics().setSeverity(
clang::diag::warn_unused_expr, diag::Severity::Ignored,
SourceLocation());
+ // Suppress [[nodiscard]] warnings during parsing since we don't know yet
+ // if the expression has a missing semicolon (value printed) or not.
+ // If the value is printed, it's considered "used" so no warning is needed.
+ getCompilerInstance()->getDiagnostics().setSeverity(
+ clang::diag::warn_unused_result, diag::Severity::Ignored,
+ SourceLocation());
llvm::Expected<TranslationUnitDecl *> TuOrErr = IncrParser->Parse(Code);
if (!TuOrErr)
return TuOrErr.takeError();
+ // After parsing, re-enable [[nodiscard]] warnings and diagnose for
----------------
bala-bhargav wrote:
We can't mark the expression as 'used' at parse time because the [[nodiscard]]
diagnostic is emitted inside
ActOnExprStmt
with DiscardedValue=true during parsing, but the semicolon detection
(isSemiMissing) happens AFTER parsing completes. These two events occur in
different phases of the compilation pipeline — the diagnostic fires in Sema
during statement creation, while the semicolon status is determined in the
Parser after the statement is fully consumed. Since we don't know whether the
expression result will be printed (used) or discarded until after parsing, we
can't set DiscardedValue=false at the right time. To do that, we would need to
restructure the parser to detect the semicolon before invoking Sema's
expression statement handling, which is a significantly deeper change to the
parsing flow.
https://github.com/llvm/llvm-project/pull/178661
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits