Skip to content

Commit 8b3494e

Browse files
committed
Added comments.
1 parent 9f7fca0 commit 8b3494e

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

source/Main.cpp

+52-41
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ int RunCheckFormFile(namelint::MemoBoard &Memo);
3131
LOG_DECISION_DEFAULT(false);
3232

3333
int RunCheckFormFile(namelint::MemoBoard &Memo) {
34-
int iRet = 0;
3534

3635
if (!llvm::sys::fs::exists(Memo.File.Source)) {
37-
cout << "ERROR: Failed to find input source file." << endl;
36+
cout << "Error: Failed to find input source file." << endl;
3837
return 1;
3938
}
4039

@@ -43,21 +42,28 @@ int RunCheckFormFile(namelint::MemoBoard &Memo) {
4342
//
4443
std::string ErrorMessage;
4544
auto Compilations = FixedCompilationDatabase::loadFromFile(Memo.File.Source, ErrorMessage);
45+
if (nullptr == Compilations) {
46+
printf("Error: Failed to load and convert to compliation database.\n");
47+
printf(" (%s) \n", ErrorMessage.c_str());
48+
return 2;
49+
}
4650

4751
//
4852
// Create clang tool then add clang tool arguments.
4953
//
5054
vector<string> SingleFileInList = {Memo.File.Source};
5155
ClangTool Tool(*Compilations, SingleFileInList);
5256

53-
RunCheck(Memo, Tool);
57+
//
58+
// Execute `check` command.
59+
//
60+
int iRet = RunCheck(Memo, Tool);
61+
5462
return iRet;
5563
}
5664

5765
int RunCheckFormStream(namelint::MemoBoard &Memo, const string &SourceContent,
5866
const string &VirtFileName) {
59-
int iRet = 0;
60-
6167
//
6268
// Load source code file then create compilatation database.
6369
//
@@ -70,31 +76,13 @@ int RunCheckFormStream(namelint::MemoBoard &Memo, const string &SourceContent,
7076
ClangTool Tool(Compilations, std::vector<std::string>(1, VirtFileName));
7177
Tool.mapVirtualFile(VirtFileName, SourceContent);
7278

73-
iRet = RunCheck(Memo, Tool);
79+
//
80+
// Execute `check` command.
81+
int iRet = RunCheck(Memo, Tool);
82+
7483
return iRet;
7584
}
7685

77-
// int RunCheckFormStream(namelint::MemoBoard &Memo, const vector<string> &SourceContentList,
78-
// const string &VirtFileName) {
79-
// int iRet = 0;
80-
//
81-
// //
82-
// // Load source code file then create compilatation database.
83-
// //
84-
// std::string ErrorMessage;
85-
// FixedCompilationDatabase Compilations("./", std::vector<std::string>());
86-
//
87-
// //
88-
// // Create clang tool then add clang tool arguments.
89-
// //
90-
// ClangTool Tool(Compilations, SourceContentList);
91-
// Tool.mapVirtualFile("aa.cpp", SourceContentList[0]);
92-
// Tool.mapVirtualFile("aa.h", SourceContentList[1]);
93-
//
94-
// iRet = RunCheck(Memo, Tool);
95-
// return iRet;
96-
//}
97-
9886
int RunCheck(namelint::MemoBoard &Memo, ClangTool &Tool) {
9987
int iRet = 0;
10088

@@ -111,6 +99,9 @@ int RunCheck(namelint::MemoBoard &Memo, ClangTool &Tool) {
11199
DcLib::Log::Out(INFO_ALL, "Config File = %s", Memo.File.Config.c_str());
112100
DcLib::Log::Out(INFO_ALL, "OutputJson = %s", OutputJson.c_str());
113101

102+
//
103+
// Feed header directories from input commands to ClangTools'.
104+
//
114105
for (auto inc : Memo.Dir.Includes) {
115106
llvm::SmallString<128> IncDirPath(inc);
116107
llvm::sys::fs::make_absolute(IncDirPath);
@@ -119,39 +110,58 @@ int RunCheck(namelint::MemoBoard &Memo, ClangTool &Tool) {
119110
DcLib::Log::Out(INFO_ALL, "-I %s", IncDirPath.c_str());
120111
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster(inc_dir, ArgumentInsertPosition::BEGIN));
121112
}
122-
// Tool.appendArgumentsAdjuster(
123-
// getInsertArgumentAdjuster("-E",
124-
// ArgumentInsertPosition::BEGIN));
125-
126-
// Tool.appendArgumentsAdjuster(
127-
// getInsertArgumentAdjuster("-v", ArgumentInsertPosition::BEGIN));
128-
Tool.appendArgumentsAdjuster(
129-
getInsertArgumentAdjuster("--language=c++",
130-
ArgumentInsertPosition::BEGIN)); // Make it parses header file.
113+
114+
//
115+
// Add `-E` option (Only run the preprocessor) to ClantTool.
116+
//
117+
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster("-E", // Run the preprocessor
118+
ArgumentInsertPosition::BEGIN));
119+
120+
//
121+
// Add `verbose` option to ClantTool.
122+
//
123+
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster("-v", // Verbose
124+
ArgumentInsertPosition::BEGIN));
125+
126+
//
127+
// Make it parses header file.
128+
//
129+
Tool.appendArgumentsAdjuster(getInsertArgumentAdjuster("--language=c++", // C++
130+
ArgumentInsertPosition::BEGIN));
131131
DcLib::Log::Out(INFO_ALL, "--language=c++");
132132

133+
//
134+
// Bypass DiagnosticConsumer to disable diagnostic feature.
135+
//
133136
Tool.setDiagnosticConsumer(new IgnoringDiagConsumer());
134137

135-
Detection Detect;
138+
//
139+
// Check file name if config option is ENABLED.
140+
//
136141
shared_ptr<ConfigData> pConfig = Memo.Config.GetData();
137142
GeneralOptions *pOptions = &pConfig->General.Options;
138143
if (!pOptions->bCheckFileName) {
139144
DcLib::Log::Out(INFO_ALL, "Skipped, becuase config file is disable. (bCheckFileName)");
140145
} else {
141146
Memo.Checked.nFile++;
142147

143-
string FileBaseName = Path::FindFileName(Memo.File.Source);
148+
string FileBaseName = Path::FindFileName(Memo.File.Source);
149+
150+
Detection Detect;
144151
GeneralRules *pRules = &pConfig->General.Rules;
145152
if (!Detect.CheckFile(pRules->FileName, FileBaseName)) {
146153
Memo.Error.nFile++;
147154
Memo.ErrorDetailList.push_back(new ErrorDetail(FileBaseName, ""));
148155
}
149156
}
150157

158+
//
159+
// Check input source file except file name which are possiblely did above.
160+
//
151161
MyFactory MyFactory;
152162
std::unique_ptr<FrontendActionFactory> Factory = newFrontendActionFactory(&MyFactory);
153163

154-
// Go
164+
// Go with ClangTool
155165
if (0 == Tool.run(Factory.get())) {
156166
iRet = GetTotalError(Memo);
157167
if (Memo.Config.GetData()->General.Options.bAllowedPrintResult) {
@@ -279,12 +289,12 @@ int main(int Argc, const char **Argv) {
279289

280290
if (!CheckInputConfig.empty()) {
281291
if (!llvm::sys::fs::exists(CheckInputConfig)) {
282-
cout << "ERROR: Failed to find config file." << endl;
292+
cout << "Error: Failed to find config file." << endl;
283293
return 2;
284294
}
285295
string ErrorReason;
286296
if (!pAppCxt->MemoBoard.Config.LoadFile(CheckInputConfig, ErrorReason)) {
287-
cout << "ERROR: Failed to load config file (format wrong)." << endl;
297+
cout << "Error: Failed to load config file (format wrong)." << endl;
288298
cout << ErrorReason << endl;
289299
return 3;
290300
}
@@ -310,6 +320,7 @@ int main(int Argc, const char **Argv) {
310320

311321
} else if (TestSubcommand) {
312322
iRet = RunTest(TestOutputJson, TestNameFilter);
323+
313324
} else {
314325
iRet = -1; /* Error (Command miss matched.) */
315326
cl::PrintHelpMessage();

0 commit comments

Comments
 (0)