diff options
| author | Reid Kleckner <rnk@google.com> | 2018-03-08 00:14:34 +0000 |
|---|---|---|
| committer | Reid Kleckner <rnk@google.com> | 2018-03-08 00:14:34 +0000 |
| commit | c2fd3529630d24e3b561f8d6dd5fca282616ad34 (patch) | |
| tree | a6205b2ebc5e05721ab89ca9ab401ccc90655fa6 /clang/lib/Sema/AnalysisBasedWarnings.cpp | |
| parent | c4a13015fd6b83b802cd830f8adc9693d6392c48 (diff) | |
| download | bcm5719-llvm-c2fd3529630d24e3b561f8d6dd5fca282616ad34.tar.gz bcm5719-llvm-c2fd3529630d24e3b561f8d6dd5fca282616ad34.zip | |
[Sema] Make getCurFunction() return null outside function parsing
Summary:
Before this patch, Sema pre-allocated a FunctionScopeInfo and kept it in
the first, always present element of the FunctionScopes stack. This
meant that Sema::getCurFunction would return a pointer to this
pre-allocated object when parsing code outside a function body. This is
pretty much always a bug, so this patch moves the pre-allocated object
into a separate unique_ptr. This should make bugs like PR36536 a lot
more obvious.
As you can see from this patch, there were a number of places that
unconditionally assumed they were always called inside a function.
However, there are also many places that null checked the result of
getCurFunction(), so I think this is a reasonable direction.
Reviewers: rsmith
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D44039
llvm-svn: 326965
Diffstat (limited to 'clang/lib/Sema/AnalysisBasedWarnings.cpp')
| -rw-r--r-- | clang/lib/Sema/AnalysisBasedWarnings.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 9c9e1c34f2c..87f73f75751 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -632,18 +632,19 @@ struct CheckFallThroughDiagnostics { } // anonymous namespace -/// CheckFallThroughForFunctionDef - Check that we don't fall off the end of a +/// CheckFallThroughForBody - Check that we don't fall off the end of a /// function that should return a value. Check that we don't fall off the end /// of a noreturn function. We assume that functions and blocks not marked /// noreturn will return. static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, const BlockExpr *blkExpr, - const CheckFallThroughDiagnostics& CD, - AnalysisDeclContext &AC) { + const CheckFallThroughDiagnostics &CD, + AnalysisDeclContext &AC, + sema::FunctionScopeInfo *FSI) { bool ReturnsVoid = false; bool HasNoReturn = false; - bool IsCoroutine = S.getCurFunction() && S.getCurFunction()->isCoroutine(); + bool IsCoroutine = FSI->isCoroutine(); if (const auto *FD = dyn_cast<FunctionDecl>(D)) { if (const auto *CBody = dyn_cast<CoroutineBodyStmt>(Body)) @@ -675,7 +676,7 @@ static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, SourceLocation LBrace = Body->getLocStart(), RBrace = Body->getLocEnd(); auto EmitDiag = [&](SourceLocation Loc, unsigned DiagID) { if (IsCoroutine) - S.Diag(Loc, DiagID) << S.getCurFunction()->CoroutinePromise->getType(); + S.Diag(Loc, DiagID) << FSI->CoroutinePromise->getType(); else S.Diag(Loc, DiagID); }; @@ -2143,7 +2144,7 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, : (fscope->isCoroutine() ? CheckFallThroughDiagnostics::MakeForCoroutine(D) : CheckFallThroughDiagnostics::MakeForFunction(D))); - CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC); + CheckFallThroughForBody(S, D, Body, blkExpr, CD, AC, fscope); } // Warning: check for unreachable code |

