Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / commaoperator.cxx
blob0693fc83a07b0d2c1788b88c7a4a376e6ae14932
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
15 #include "plugin.hxx"
17 /**
18 the comma operator is best used sparingly
21 namespace {
23 Stmt const * lookThroughExprWithCleanups(Stmt const * stmt) {
24 if (auto const e = dyn_cast_or_null<ExprWithCleanups>(stmt)) {
25 return e->getSubExpr();
27 return stmt;
30 class CommaOperator:
31 public RecursiveASTVisitor<CommaOperator>, public loplugin::Plugin
33 public:
34 explicit CommaOperator(loplugin::InstantiationData const & data):
35 Plugin(data) {}
37 virtual void run() override
39 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
42 bool TraverseForStmt(ForStmt * stmt) {
43 auto const saved1 = ignore1_;
44 ignore1_ = lookThroughExprWithCleanups(stmt->getInit());
45 auto const saved2 = ignore2_;
46 ignore2_ = lookThroughExprWithCleanups(stmt->getInc());
47 auto const ret = RecursiveASTVisitor::TraverseForStmt(stmt);
48 ignore1_ = saved1;
49 ignore2_ = saved2;
50 return ret;
53 bool TraverseWhileStmt(WhileStmt * stmt) {
54 auto const saved1 = ignore1_;
55 ignore1_ = lookThroughExprWithCleanups(stmt->getCond());
56 auto const ret = RecursiveASTVisitor::TraverseWhileStmt(stmt);
57 ignore1_ = saved1;
58 return ret;
61 bool TraverseParenExpr(ParenExpr * expr) {
62 auto const saved1 = ignore1_;
63 ignore1_ = expr->getSubExpr();
64 auto const ret = RecursiveASTVisitor::TraverseParenExpr(expr);
65 ignore1_ = saved1;
66 return ret;
69 bool TraverseBinComma(BinaryOperator * expr) {
70 if (!WalkUpFromBinComma(expr)) {
71 return false;
73 auto const saved1 = ignore1_;
74 ignore1_ = expr->getLHS();
75 auto const ret = TraverseStmt(expr->getLHS())
76 && TraverseStmt(expr->getRHS());
77 ignore1_ = saved1;
78 return ret;
81 bool VisitBinComma(const BinaryOperator* );
83 private:
84 Stmt const * ignore1_ = nullptr;
85 Stmt const * ignore2_ = nullptr;
88 bool CommaOperator::VisitBinComma(const BinaryOperator* binaryOp)
90 if (binaryOp == ignore1_ || binaryOp == ignore2_) {
91 return true;
93 if (ignoreLocation(binaryOp)) {
94 return true;
96 // Ignore FD_SET expanding to "...} while(0, 0)" in some Microsoft
97 // winsock2.h (TODO: improve heuristic of determining that the whole
98 // binaryOp is part of a single macro body expansion):
99 if (compiler.getSourceManager().isMacroBodyExpansion(
100 binaryOp->getLocStart())
101 && compiler.getSourceManager().isMacroBodyExpansion(
102 binaryOp->getOperatorLoc())
103 && compiler.getSourceManager().isMacroBodyExpansion(
104 binaryOp->getLocEnd())
105 && ignoreLocation(
106 compiler.getSourceManager().getSpellingLoc(
107 binaryOp->getOperatorLoc())))
109 return true;
111 report(
112 DiagnosticsEngine::Warning, "comma operator hides code",
113 binaryOp->getOperatorLoc())
114 << binaryOp->getSourceRange();
115 return true;
119 loplugin::Plugin::Registration< CommaOperator > X("commaoperator", true);
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */