summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2016-03-30 18:59:54 +0200
committerStephan Bergmann <sbergman@redhat.com>2016-03-30 18:59:54 +0200
commitd2f9f27774ec138c9f66c55f582a123d8ebd19ff (patch)
treeb984aa02aab3813d2a9c0ede9ad1e5d0fdf7fdb1 /compilerplugins
parent14060e76e33cfb305c1469fecf7db688bf8a8858 (diff)
loplugin:nullptr: Find some more cases in templates
Change-Id: I1f127d56e40b04f2b4df85c0afbcfd424d68a8cc
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/nullptr.cxx72
1 files changed, 62 insertions, 10 deletions
diff --git a/compilerplugins/clang/nullptr.cxx b/compilerplugins/clang/nullptr.cxx
index 800343963b60..1e6311b1862b 100644
--- a/compilerplugins/clang/nullptr.cxx
+++ b/compilerplugins/clang/nullptr.cxx
@@ -51,6 +51,10 @@ public:
bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const * expr);
+ bool VisitParmVarDecl(ParmVarDecl const * decl);
+
+ bool TraverseConstructorInitializer(CXXCtorInitializer * init);
+
// bool shouldVisitTemplateInstantiations() const { return true; }
private:
@@ -60,6 +64,10 @@ private:
bool isMacroBodyExpansion(SourceLocation location) const;
+ void visitCXXCtorInitializer(CXXCtorInitializer const * init);
+
+ void handleZero(Expr const * expr);
+
void handleNull(
Expr const * expr, char const * castKind,
Expr::NullPointerConstantKind nullPointerKind);
@@ -142,12 +150,7 @@ bool Nullptr::VisitBinaryOperator(BinaryOperator const * expr) {
default:
return true;
}
- //TODO: detect NPCK_ZeroExpression where appropriate
- auto const lit = dyn_cast<IntegerLiteral>(e->IgnoreParenImpCasts());
- if (lit == nullptr || lit->getValue().getBoolValue()) {
- return true;
- }
- handleNull(e, nullptr, Expr::NPCK_ZeroLiteral);
+ handleZero(e);
return true;
}
@@ -173,15 +176,30 @@ bool Nullptr::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const * expr) {
default:
return true;
}
- //TODO: detect NPCK_ZeroExpression where appropriate
- auto const lit = dyn_cast<IntegerLiteral>(e->IgnoreParenImpCasts());
- if (lit == nullptr || lit->getValue().getBoolValue()) {
+ handleZero(e);
+ return true;
+}
+
+bool Nullptr::VisitParmVarDecl(ParmVarDecl const * decl) {
+ if (ignoreLocation(decl)) {
+ return true;
+ }
+ if (!decl->getType()->isPointerType()) {
+ return true;
+ }
+ auto e = decl->getDefaultArg();
+ if (e == nullptr) {
return true;
}
- handleNull(e, nullptr, Expr::NPCK_ZeroLiteral);
+ handleZero(e);
return true;
}
+bool Nullptr::TraverseConstructorInitializer(CXXCtorInitializer * init) {
+ visitCXXCtorInitializer(init);
+ return RecursiveASTVisitor::TraverseConstructorInitializer(init);
+}
+
bool Nullptr::isInLokIncludeFile(SourceLocation spellingLocation) const {
return compiler.getSourceManager().getFilename(spellingLocation)
.startswith(SRCDIR "/include/LibreOfficeKit/");
@@ -204,6 +222,40 @@ bool Nullptr::isMacroBodyExpansion(SourceLocation location) const {
#endif
}
+void Nullptr::visitCXXCtorInitializer(CXXCtorInitializer const * init) {
+ if (!init->isWritten()) {
+ return;
+ }
+ auto e = init->getInit();
+ if (ignoreLocation(e)) {
+ return;
+ }
+ auto d = init->getAnyMember();
+ if (d == nullptr || !d->getType()->isPointerType()) {
+ return;
+ }
+ if (auto e2 = dyn_cast<ParenListExpr>(e)) {
+ if (e2->getNumExprs() != 1) {
+ return;
+ }
+ e = e2->getExpr(0);
+ } else if (auto e2 = dyn_cast<InitListExpr>(e)) {
+ if (e2->getNumInits() != 1) {
+ return;
+ }
+ e = e2->getInit(0);
+ }
+ handleZero(e);
+}
+
+void Nullptr::handleZero(Expr const * expr) {
+ //TODO: detect NPCK_ZeroExpression where appropriate
+ auto const lit = dyn_cast<IntegerLiteral>(expr->IgnoreParenImpCasts());
+ if (lit != nullptr && !lit->getValue().getBoolValue()) {
+ handleNull(expr, nullptr, Expr::NPCK_ZeroLiteral);
+ }
+}
+
void Nullptr::handleNull(
Expr const * expr, char const * castKind,
Expr::NullPointerConstantKind nullPointerKind)