Cppcheck
executor.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "executor.h"
20 
21 #include "color.h"
22 #include "errorlogger.h"
23 #include "library.h"
24 #include "settings.h"
25 #include "suppressions.h"
26 
27 #include <cassert>
28 #include <sstream>
29 #include <utility>
30 
31 struct FileSettings;
32 
33 Executor::Executor(const std::list<FileWithDetails> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, SuppressionList &suppressions, ErrorLogger &errorLogger)
34  : mFiles(files), mFileSettings(fileSettings), mSettings(settings), mSuppressions(suppressions), mErrorLogger(errorLogger)
35 {
36  // the two inputs may only be used exclusively
37  assert(!(!files.empty() && !fileSettings.empty()));
38 }
39 
40 // TODO: this logic is duplicated in CppCheck::reportErr()
42 {
44  return false;
45 
46  if (!mSuppressions.isSuppressed(msg, {}))
47  {
48  // TODO: there should be no need for verbose and default messages here
49  std::string errmsg = msg.toString(mSettings.verbose);
50  if (errmsg.empty())
51  return false;
52 
53  std::lock_guard<std::mutex> lg(mErrorListSync);
54  if (mErrorList.emplace(std::move(errmsg)).second) {
55  return true;
56  }
57  }
58  return false;
59 }
60 
61 void Executor::reportStatus(std::size_t fileindex, std::size_t filecount, std::size_t sizedone, std::size_t sizetotal)
62 {
63  if (filecount > 1) {
64  std::ostringstream oss;
65  const unsigned long percentDone = (sizetotal > 0) ? (100 * sizedone) / sizetotal : 0;
66  oss << fileindex << '/' << filecount
67  << " files checked " << percentDone
68  << "% done";
70  }
71 }
72 
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
virtual void reportOut(const std::string &outmsg, Color c=Color::Reset)=0
Information about progress is directed here.
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
std::string toString(bool verbose, const std::string &templateFormat=emptyString, const std::string &templateLocation=emptyString) const
Format the error message into a string.
std::string file0
For GUI rechecking; source file (not header)
Definition: errorlogger.h:168
bool hasToLog(const ErrorMessage &msg)
Check if message is being suppressed and unique.
Definition: executor.cpp:41
std::unordered_set< std::string > mErrorList
Definition: executor.h:80
Executor(const std::list< FileWithDetails > &files, const std::list< FileSettings > &fileSettings, const Settings &settings, SuppressionList &suppressions, ErrorLogger &errorLogger)
Definition: executor.cpp:33
SuppressionList & mSuppressions
Definition: executor.h:74
std::mutex mErrorListSync
Definition: executor.h:78
void reportStatus(std::size_t fileindex, std::size_t filecount, std::size_t sizedone, std::size_t sizetotal)
Information about how many files have been checked.
Definition: executor.cpp:61
ErrorLogger & mErrorLogger
Definition: executor.h:75
const Settings & mSettings
Definition: executor.h:73
bool reportErrors(const std::string &path) const
Definition: library.cpp:1604
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
Library library
Library.
Definition: settings.h:237
bool verbose
Is –verbose given?
Definition: settings.h:398
class for handling suppressions
Definition: suppressions.h:42
bool isSuppressed(const ErrorMessage &errmsg, bool global=true)
Returns true if this message should not be shown to the user.
File settings.
Definition: filesettings.h:57