forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsercontext.cpp
More file actions
109 lines (90 loc) · 2.76 KB
/
parsercontext.cpp
File metadata and controls
109 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <boost/filesystem/exception.hpp>
#include <fstream>
#include <boost/filesystem.hpp>
#include <model/file.h>
#include <model/file-odb.hxx>
#include <util/hash.h>
#include <util/odbtransaction.h>
#include <parser/parsercontext.h>
#include <parser/sourcemanager.h>
namespace po = boost::program_options;
namespace fs = boost::filesystem;
namespace cc
{
namespace parser
{
ParserContext::ParserContext(
std::shared_ptr<odb::database> db_,
SourceManager& srcMgr_,
std::string& compassRoot_,
po::variables_map& options_) :
db(db_),
srcMgr(srcMgr_),
compassRoot(compassRoot_),
options(options_)
{
std::unordered_map<std::string, std::string> fileHashes;
(util::OdbTransaction(this->db))([&]
{
// Fetch directory and binary type files from SourceManager
auto func = [](model::FilePtr item)
{
return item->type != model::File::DIRECTORY_TYPE &&
item->type != model::File::BINARY_TYPE;
};
std::vector<model::FilePtr> files = this->srcMgr.getFiles(func);
for (model::FilePtr file : files)
{
if (boost::filesystem::exists(file->path))
{
if (!fileStatus.count(file->path))
{
model::FileContentPtr content = file->content.load();
if (!content)
continue;
fileHashes[file->path] = content->hash;
std::ifstream fileStream(file->path);
std::string fileContent(
std::istreambuf_iterator<char>{fileStream},
std::istreambuf_iterator<char>{});
fileStream.close();
if (content->hash != util::sha1Hash(fileContent))
{
this->fileStatus.emplace(
file->path, cc::parser::IncrementalStatus::MODIFIED);
LOG(debug) << "File modified: " << file->path;
}
}
}
else
{
fileStatus.emplace(
file->path, cc::parser::IncrementalStatus::DELETED);
LOG(debug) << "File deleted: " << file->path;
}
}
// TODO: detect ADDED files
});
// Fill moduleDirectories vector
if (options.count("modules")) {
const std::string& modulesFilePath = options["modules"].as<std::string>();
std::ifstream fileStream(modulesFilePath);
if (!fileStream.good()) {
LOG(error) << "Failed to open modules file: " << modulesFilePath;
return;
}
LOG(info) << "Processing modules file: " << modulesFilePath;
std::string line;
while (std::getline(fileStream, line)) {
try {
const fs::path p = fs::canonical(line);
moduleDirectories.push_back(p.string());
} catch (fs::filesystem_error& err) {
LOG(error) << "Failed to process path from modules file: " << line;
LOG(error) << err.what();
}
}
}
}
}
}