From 86e5cb0e0bc938c9cf44d91eeffdce31ab5cb3ee Mon Sep 17 00:00:00 2001 From: Shuai Wang Date: Wed, 19 Sep 2018 18:00:55 +0000 Subject: [PATCH] [analyzer] Fix nullptr access when processing instantiated function in ExprMutationAnalyzer. llvm-svn: 342562 --- clang/lib/Analysis/ExprMutationAnalyzer.cpp | 2 +- .../Analysis/ExprMutationAnalyzerTest.cpp | 30 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index db8c259..8414cb5 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -379,7 +379,7 @@ const Stmt *ExprMutationAnalyzer::findFunctionArgMutation(const Expr *Exp) { for (const auto &Nodes : Matches) { const auto *Exp = Nodes.getNodeAs(NodeID::value); const auto *Func = Nodes.getNodeAs("func"); - if (!Func->getBody()) + if (!Func->getBody() || !Func->getPrimaryTemplate()) return Exp; const auto *Parm = Nodes.getNodeAs("parm"); diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index 30950e0..8823ac8 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -215,6 +215,12 @@ TEST(ExprMutationAnalyzerTest, ByValueArgument) { "void f() { A x, y; y = x; }"); Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_FALSE(isMutated(Results, AST.get())); + + AST = buildASTFromCode( + "template struct A { A(); A(const A&); static void mf(A) {} };" + "void f() { A<0> x; A<0>::mf(x); }"); + Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_FALSE(isMutated(Results, AST.get())); } TEST(ExprMutationAnalyzerTest, ByConstValueArgument) { @@ -241,6 +247,12 @@ TEST(ExprMutationAnalyzerTest, ByConstValueArgument) { "void f() { struct A { A(const int); }; int x; A y(x); }"); Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_FALSE(isMutated(Results, AST.get())); + + AST = buildASTFromCode("template struct A { A(); A(const A&);" + "static void mf(const A&) {} };" + "void f() { A<0> x; A<0>::mf(x); }"); + Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_FALSE(isMutated(Results, AST.get())); } TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) { @@ -288,6 +300,12 @@ TEST(ExprMutationAnalyzerTest, ByNonConstRefArgument) { AST = buildASTFromCode("void f() { struct A { A(); A(A&); }; A x; A y(x); }"); Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); + + AST = buildASTFromCode( + "template struct A { A(); A(const A&); static void mf(A&) {} };" + "void f() { A<0> x; A<0>::mf(x); }"); + Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("A<0>::mf(x)")); } TEST(ExprMutationAnalyzerTest, ByConstRefArgument) { @@ -686,6 +704,12 @@ TEST(ExprMutationAnalyzerTest, FollowFuncArgModified) { Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); + AST = buildASTFromCode("template struct S {" + "template S(T&& t) : m(++t) { } U m; };" + "void f() { int x; S s(x); }"); + Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x")); + AST = buildASTFromCode(StdRemoveReference + StdForward + "template void u(Args&...);" "template void h(Args&&... args)" @@ -737,6 +761,12 @@ TEST(ExprMutationAnalyzerTest, FollowFuncArgNotModified) { Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_FALSE(isMutated(Results, AST.get())); + AST = buildASTFromCode("template struct S {" + "template S(T&& t) : m(t) { } U m; };" + "void f() { int x; S s(x); }"); + Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_FALSE(isMutated(Results, AST.get())); + AST = buildASTFromCode(StdRemoveReference + StdForward + "template void u(Args...);" "template void h(Args&&... args)" -- 2.7.4