================
@@ -1620,6 +1620,43 @@ void Sema::ActOnEndOfTranslationUnit() {
   if (Context.hasAnyFunctionEffects())
     performFunctionEffectAnalysis(Context.getTranslationUnitDecl());
 
+  // Diagnose unused-but-set static globals in a deterministic order.
+  // Not tracking shadowing info for static globals; there's nothing to shadow.
+  struct LocAndDiag {
+    SourceLocation Loc;
+    PartialDiagnostic PD;
+  };
+  SmallVector<LocAndDiag, 16> DeclDiags;
+  auto addDiag = [&DeclDiags](SourceLocation Loc, PartialDiagnostic PD) {
+    DeclDiags.push_back(LocAndDiag{Loc, std::move(PD)});
+  };
+
+  // For -Wunused-but-set-variable we only care about variables that were
+  // referenced by the TU end.
+  SmallVector<const VarDecl *, 16> DeclsToErase;
+  for (const auto &Ref : RefsMinusAssignments) {
+    const VarDecl *VD = Ref.first;
+    // Only diagnose static file vars defined in the main file to match
+    // -Wunused-variable behavior and avoid false positives from header vars.
+    if (VD->isStaticFileVar() && SourceMgr.isInMainFile(VD->getLocation())) {
+      DiagnoseUnusedButSetDecl(VD, addDiag);
+      DeclsToErase.push_back(VD);
+    }
+  }
+  for (const VarDecl *VD : DeclsToErase) {
+    RefsMinusAssignments.erase(VD);
+  }
+
+  llvm::sort(DeclDiags,
+             [](const LocAndDiag &LHS, const LocAndDiag &RHS) -> bool {
+               // Sorting purely for determinism; matches behavior in
+               // SemaDecl.cpp.
+               return LHS.Loc.getRawEncoding() < RHS.Loc.getRawEncoding();
----------------
zwuis wrote:

- Is it necessary to sort diagnostic messages? Why?
- Comparing `SourceLocation` definitely doesn't work with macros.

https://github.com/llvm/llvm-project/pull/178342
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to