@@ -1912,14 +1912,6 @@ bool Sema::mightHaveNonExternalLinkage(const DeclaratorDecl *D) {
19121912 return !D->isExternallyVisible();
19131913}
19141914
1915- // FIXME: This needs to be refactored; some other isInMainFile users want
1916- // these semantics.
1917- static bool isMainFileLoc(const Sema &S, SourceLocation Loc) {
1918- if (S.TUKind != TU_Complete || S.getLangOpts().IsHeaderFile)
1919- return false;
1920- return S.SourceMgr.isInMainFile(Loc);
1921- }
1922-
19231915bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
19241916 assert(D);
19251917
@@ -1946,7 +1938,7 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
19461938 return false;
19471939 } else {
19481940 // 'static inline' functions are defined in headers; don't warn.
1949- if (FD->isInlined() && !isMainFileLoc(*this, FD->getLocation()))
1941+ if (FD->isInlined() && !isMainFileLoc(FD->getLocation()))
19501942 return false;
19511943 }
19521944
@@ -1957,7 +1949,7 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
19571949 // Constants and utility variables are defined in headers with internal
19581950 // linkage; don't warn. (Unlike functions, there isn't a convenient marker
19591951 // like "inline".)
1960- if (!isMainFileLoc(*this, VD->getLocation()))
1952+ if (!isMainFileLoc(VD->getLocation()))
19611953 return false;
19621954
19631955 if (Context.DeclMustBeEmitted(VD))
@@ -1971,7 +1963,7 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
19711963 VD->getMemberSpecializationInfo() && !VD->isOutOfLine())
19721964 return false;
19731965
1974- if (VD->isInline() && !isMainFileLoc(*this, VD->getLocation()))
1966+ if (VD->isInline() && !isMainFileLoc(VD->getLocation()))
19751967 return false;
19761968 } else {
19771969 return false;
@@ -2215,6 +2207,11 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD,
22152207 return;
22162208 }
22172209
2210+ // Don't warn on volatile file-scope variables. They are visible beyond their
2211+ // declaring function and writes to them could be observable side effects.
2212+ if (VD->getType().isVolatileQualified() && VD->isFileVarDecl())
2213+ return;
2214+
22182215 // Don't warn about __block Objective-C pointer variables, as they might
22192216 // be assigned in the block but not used elsewhere for the purpose of lifetime
22202217 // extension.
@@ -2227,7 +2224,7 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD,
22272224 if (VD->hasAttr<ObjCPreciseLifetimeAttr>() && Ty->isObjCObjectPointerType())
22282225 return;
22292226
2230- auto iter = RefsMinusAssignments.find(VD);
2227+ auto iter = RefsMinusAssignments.find(VD->getCanonicalDecl() );
22312228 if (iter == RefsMinusAssignments.end())
22322229 return;
22332230
@@ -2305,9 +2302,11 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
23052302 DiagnoseUnusedDecl(D, addDiag);
23062303 if (const auto *RD = dyn_cast<RecordDecl>(D))
23072304 DiagnoseUnusedNestedTypedefs(RD, addDiag);
2308- if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
2305+ // Wait until end of TU to diagnose internal linkage file vars.
2306+ if (auto *VD = dyn_cast<VarDecl>(D);
2307+ VD && !VD->isInternalLinkageFileVar()) {
23092308 DiagnoseUnusedButSetDecl(VD, addDiag);
2310- RefsMinusAssignments.erase(VD);
2309+ RefsMinusAssignments.erase(VD->getCanonicalDecl() );
23112310 }
23122311 }
23132312
0 commit comments