summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/nullptr.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'compilerplugins/clang/nullptr.cxx')
-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)