Coverage Report

Created: 2023-10-30 17:15

/builds/2mk6rsew/0/parcoach/parcoach/src/aSSA/SonarSerializationPass.cpp
Line
Count
Source
1
#include "parcoach/SonarSerializationPass.h"
2
3
#include "parcoach/CollListFunctionAnalysis.h"
4
#include "parcoach/Options.h"
5
#include "parcoach/SerializableWarning.h"
6
7
#include "Config.h"
8
9
#include "llvm/Support/CommandLine.h"
10
#include "llvm/Support/FileSystem.h"
11
#include "llvm/Support/JSON.h"
12
13
using namespace llvm;
14
15
namespace parcoach {
16
namespace {
17
cl::opt<std::string> DatabaseFile("analysis-db",
18
                                  cl::desc("File to store the warnings"),
19
                                  cl::cat(ParcoachCategory));
20
21
}
22
23
namespace serialization::sonar {
24
4
Optional<Database> loadDatabase() {
25
4
  assert(!DatabaseFile.empty() &&
26
4
         "This function assumes there is a file to load");
27
28
4
  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
29
4
      MemoryBuffer::getFileOrSTDIN(DatabaseFile, /*IsText=*/true);
30
4
  if (std::error_code EC = FileOrErr.getError()) {
31
1
    SMDiagnostic(DatabaseFile, SourceMgr::DK_Note,
32
1
                 EC.message() + ". Database will be created from scratch.")
33
1
        .print(ProgName, errs(), true, true);
34
1
    return Database{};
35
1
  }
36
3
  auto DB = Database::load((*FileOrErr)->getBuffer());
37
3
  if (auto E = DB.takeError()) {
38
1
    SMDiagnostic(DatabaseFile, SourceMgr::DK_Error,
39
1
                 "Could not load analysis database, no results will be save. " +
40
1
                     toString(std::move(E)))
41
1
        .print(ProgName, errs(), true, true);
42
1
    return {};
43
1
  }
44
2
  return std::move(*DB);
45
3
}
46
47
3
bool saveDatabase(Database const &DB) {
48
3
  assert(!DatabaseFile.empty() &&
49
3
         "This function assumes there is a file where to save the database");
50
3
  std::error_code EC;
51
3
  raw_fd_ostream Os{DatabaseFile, EC, sys::fs::OF_TextWithCRLF};
52
3
  if (EC) {
53
1
    errs() << "Could not open file: " << EC.message() << ", " << DatabaseFile
54
1
           << "\n";
55
1
    return false;
56
1
  }
57
58
2
  DB.write(Os);
59
2
  return true;
60
3
}
61
62
} // namespace serialization::sonar
63
64
PreservedAnalyses SonarSerializationPass::run(Module &M,
65
1.76k
                                              ModuleAnalysisManager &AM) {
66
1.76k
  if (DatabaseFile.empty()) {
67
1.75k
    return PreservedAnalyses::all();
68
1.75k
  }
69
70
4
  auto &Res = AM.getResult<CollectiveAnalysis>(M);
71
72
4
  auto DB = serialization::sonar::loadDatabase();
73
4
  if (!DB) {
74
1
    return PreservedAnalyses::all();
75
1
  }
76
3
  DB->append(*Res);
77
3
  serialization::sonar::saveDatabase(*DB);
78
3
  return PreservedAnalyses::all();
79
4
}
80
81
} // namespace parcoach