Skip to content

Commit 684d5fc

Browse files
committed
Enable exceptions on AnalyzerInformation::mOutputStream
1 parent 30de4e4 commit 684d5fc

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

lib/analyzerinfo.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,28 @@
2323
#include "utils.h"
2424

2525
#include <tinyxml2.h>
26+
#include <cassert>
2627
#include <cstring>
2728
#include <map>
2829
#include <sstream> // IWYU pragma: keep
2930

31+
AnalyzerInformation::AnalyzerInformation()
32+
{
33+
mOutputStream.exceptions(std::ios_base::failbit | std::ios_base::badbit);
34+
}
35+
3036
AnalyzerInformation::~AnalyzerInformation()
3137
{
32-
close();
38+
try
39+
{
40+
close();
41+
}
42+
catch (const std::ios_base::failure&)
43+
{
44+
assert(false);
45+
46+
// TODO: Report error
47+
}
3348
}
3449

3550
static std::string getFilename(const std::string &fullpath)
@@ -66,7 +81,6 @@ void AnalyzerInformation::writeFilesTxt(const std::string &buildDir, const std::
6681

6782
void AnalyzerInformation::close()
6883
{
69-
mAnalyzerInfoFile.clear();
7084
if (mOutputStream.is_open()) {
7185
mOutputStream << "</analyzerinfo>\n";
7286
mOutputStream.close();
@@ -139,14 +153,13 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
139153
if (skipAnalysis(mAnalyzerInfoFile, hash, errors))
140154
return false;
141155

142-
// TODO: enable exceptions on the ofstream and remove all .is_open() calls?
143156
mOutputStream.open(mAnalyzerInfoFile);
144-
if (mOutputStream.is_open()) {
145-
mOutputStream << "<?xml version=\"1.0\"?>\n";
146-
mOutputStream << "<analyzerinfo hash=\"" << hash << "\">\n";
147-
} else {
148-
mAnalyzerInfoFile.clear();
149-
}
157+
// If we ever disable exceptions on mOutputStream
158+
// TODO: add this assert to all ofstreams
159+
assert(mOutputStream.is_open());
160+
161+
mOutputStream << "<?xml version=\"1.0\"?>\n";
162+
mOutputStream << "<analyzerinfo hash=\"" << hash << "\">\n";
150163

151164
return true;
152165
}

lib/analyzerinfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class ErrorMessage;
4949
*/
5050
class CPPCHECKLIB AnalyzerInformation {
5151
public:
52+
AnalyzerInformation();
5253
~AnalyzerInformation();
5354

5455
static void writeFilesTxt(const std::string &buildDir, const std::list<std::string> &sourcefiles, const std::string &userDefines, const std::list<ImportProject::FileSettings> &fileSettings);

0 commit comments

Comments
 (0)