Cppcheck
suppressions.h
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef SUPPRESSIONS_H
00020 #define SUPPRESSIONS_H
00021 
00022 #include <list>
00023 #include <string>
00024 #include <istream>
00025 #include <map>
00026 #include "config.h"
00027 
00028 /// @addtogroup Core
00029 /// @{
00030 
00031 /** @brief class for handling suppressions */
00032 class CPPCHECKLIB Suppressions {
00033 private:
00034     class CPPCHECKLIB FileMatcher {
00035         friend class Suppressions;
00036     private:
00037         /** @brief List of filenames suppressed, bool flag indicates whether suppression matched. */
00038         std::map<std::string, std::map<unsigned int, bool> > _files;
00039         /** @brief List of globs suppressed, bool flag indicates whether suppression matched. */
00040         std::map<std::string, std::map<unsigned int, bool> > _globs;
00041 
00042         /**
00043          * @brief Match a name against a glob pattern.
00044          * @param pattern The glob pattern to match.
00045          * @param name The filename to match against the glob pattern.
00046          * @return match success
00047          */
00048         static bool match(const std::string &pattern, const std::string &name);
00049 
00050     public:
00051         /**
00052          * @brief Add a file or glob (and line number).
00053          * @param name File name or glob pattern
00054          * @param line Line number
00055          * @return error message. empty upon success
00056          */
00057         std::string addFile(const std::string &name, unsigned int line);
00058 
00059         /**
00060          * @brief Returns true if the file name matches a previously added file or glob pattern.
00061          * @param file File name to check
00062          * @param line Line number
00063          * @return true if this filename/line matches
00064          */
00065         bool isSuppressed(const std::string &file, unsigned int line);
00066 
00067         /**
00068          * @brief Returns true if the file name matches a previously added file (only, not glob pattern).
00069          * @param file File name to check
00070          * @param line Line number
00071          * @return true if this filename/line matches
00072          */
00073         bool isSuppressedLocal(const std::string &file, unsigned int line);
00074     };
00075 
00076     /** @brief List of error which the user doesn't want to see. */
00077     std::map<std::string, FileMatcher> _suppressions;
00078 public:
00079     /**
00080      * @brief Don't show errors listed in the file.
00081      * @param istr Open file stream where errors can be read.
00082      * @return error message. empty upon success
00083      */
00084     std::string parseFile(std::istream &istr);
00085 
00086     /**
00087      * @brief Don't show the given error.
00088      * @param line Description of error to suppress (in id:file:line format).
00089      * @return error message. empty upon success
00090      */
00091     std::string addSuppressionLine(const std::string &line);
00092 
00093     /**
00094      * @brief Don't show this error. File and/or line are optional. In which case
00095      * the errorId alone is used for filtering.
00096      * @param errorId the id for the error, e.g. "arrayIndexOutOfBounds"
00097      * @param file File name with the path, e.g. "src/main.cpp"
00098      * @param line number, e.g. "123"
00099      * @return error message. empty upon success
00100      */
00101     std::string addSuppression(const std::string &errorId, const std::string &file = "", unsigned int line = 0);
00102 
00103     /**
00104      * @brief Returns true if this message should not be shown to the user.
00105      * @param errorId the id for the error, e.g. "arrayIndexOutOfBounds"
00106      * @param file File name with the path, e.g. "src/main.cpp"
00107      * @param line number, e.g. "123"
00108      * @return true if this error is suppressed.
00109      */
00110     bool isSuppressed(const std::string &errorId, const std::string &file, unsigned int line);
00111 
00112     /**
00113      * @brief Returns true if this message should not be shown to the user (explicit files only, not glob patterns).
00114      * @param errorId the id for the error, e.g. "arrayIndexOutOfBounds"
00115      * @param file File name with the path, e.g. "src/main.cpp"
00116      * @param line number, e.g. "123"
00117      * @return true if this error is suppressed.
00118      */
00119     bool isSuppressedLocal(const std::string &errorId, const std::string &file, unsigned int line);
00120 
00121     struct SuppressionEntry {
00122         SuppressionEntry(const std::string &aid, const std::string &afile, unsigned int aline)
00123             : id(aid), file(afile), line(aline)
00124         { }
00125 
00126         std::string id;
00127         std::string file;
00128         unsigned int line;
00129     };
00130 
00131     /**
00132      * @brief Returns list of unmatched local (per-file) suppressions.
00133      * @return list of unmatched suppressions
00134      */
00135     std::list<SuppressionEntry> getUnmatchedLocalSuppressions(const std::string &file) const;
00136 
00137     /**
00138      * @brief Returns list of unmatched global (glob pattern) suppressions.
00139      * @return list of unmatched suppressions
00140      */
00141     std::list<SuppressionEntry> getUnmatchedGlobalSuppressions() const;
00142 };
00143 
00144 /// @}
00145 
00146 #endif // SUPPRESSIONS_H