@@ -31,10 +31,9 @@ int RunCheckFormFile(namelint::MemoBoard &Memo);
31
31
LOG_DECISION_DEFAULT (false );
32
32
33
33
int RunCheckFormFile (namelint::MemoBoard &Memo) {
34
- int iRet = 0 ;
35
34
36
35
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;
38
37
return 1 ;
39
38
}
40
39
@@ -43,21 +42,28 @@ int RunCheckFormFile(namelint::MemoBoard &Memo) {
43
42
//
44
43
std::string ErrorMessage;
45
44
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
+ }
46
50
47
51
//
48
52
// Create clang tool then add clang tool arguments.
49
53
//
50
54
vector<string> SingleFileInList = {Memo.File .Source };
51
55
ClangTool Tool (*Compilations, SingleFileInList);
52
56
53
- RunCheck (Memo, Tool);
57
+ //
58
+ // Execute `check` command.
59
+ //
60
+ int iRet = RunCheck (Memo, Tool);
61
+
54
62
return iRet;
55
63
}
56
64
57
65
int RunCheckFormStream (namelint::MemoBoard &Memo, const string &SourceContent,
58
66
const string &VirtFileName) {
59
- int iRet = 0 ;
60
-
61
67
//
62
68
// Load source code file then create compilatation database.
63
69
//
@@ -70,31 +76,13 @@ int RunCheckFormStream(namelint::MemoBoard &Memo, const string &SourceContent,
70
76
ClangTool Tool (Compilations, std::vector<std::string>(1 , VirtFileName));
71
77
Tool.mapVirtualFile (VirtFileName, SourceContent);
72
78
73
- iRet = RunCheck (Memo, Tool);
79
+ //
80
+ // Execute `check` command.
81
+ int iRet = RunCheck (Memo, Tool);
82
+
74
83
return iRet;
75
84
}
76
85
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
-
98
86
int RunCheck (namelint::MemoBoard &Memo, ClangTool &Tool) {
99
87
int iRet = 0 ;
100
88
@@ -111,6 +99,9 @@ int RunCheck(namelint::MemoBoard &Memo, ClangTool &Tool) {
111
99
DcLib::Log::Out (INFO_ALL, " Config File = %s" , Memo.File .Config .c_str ());
112
100
DcLib::Log::Out (INFO_ALL, " OutputJson = %s" , OutputJson.c_str ());
113
101
102
+ //
103
+ // Feed header directories from input commands to ClangTools'.
104
+ //
114
105
for (auto inc : Memo.Dir .Includes ) {
115
106
llvm::SmallString<128 > IncDirPath (inc);
116
107
llvm::sys::fs::make_absolute (IncDirPath);
@@ -119,39 +110,58 @@ int RunCheck(namelint::MemoBoard &Memo, ClangTool &Tool) {
119
110
DcLib::Log::Out (INFO_ALL, " -I %s" , IncDirPath.c_str ());
120
111
Tool.appendArgumentsAdjuster (getInsertArgumentAdjuster (inc_dir, ArgumentInsertPosition::BEGIN));
121
112
}
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));
131
131
DcLib::Log::Out (INFO_ALL, " --language=c++" );
132
132
133
+ //
134
+ // Bypass DiagnosticConsumer to disable diagnostic feature.
135
+ //
133
136
Tool.setDiagnosticConsumer (new IgnoringDiagConsumer ());
134
137
135
- Detection Detect;
138
+ //
139
+ // Check file name if config option is ENABLED.
140
+ //
136
141
shared_ptr<ConfigData> pConfig = Memo.Config .GetData ();
137
142
GeneralOptions *pOptions = &pConfig->General .Options ;
138
143
if (!pOptions->bCheckFileName ) {
139
144
DcLib::Log::Out (INFO_ALL, " Skipped, becuase config file is disable. (bCheckFileName)" );
140
145
} else {
141
146
Memo.Checked .nFile ++;
142
147
143
- string FileBaseName = Path::FindFileName (Memo.File .Source );
148
+ string FileBaseName = Path::FindFileName (Memo.File .Source );
149
+
150
+ Detection Detect;
144
151
GeneralRules *pRules = &pConfig->General .Rules ;
145
152
if (!Detect.CheckFile (pRules->FileName , FileBaseName)) {
146
153
Memo.Error .nFile ++;
147
154
Memo.ErrorDetailList .push_back (new ErrorDetail (FileBaseName, " " ));
148
155
}
149
156
}
150
157
158
+ //
159
+ // Check input source file except file name which are possiblely did above.
160
+ //
151
161
MyFactory MyFactory;
152
162
std::unique_ptr<FrontendActionFactory> Factory = newFrontendActionFactory (&MyFactory);
153
163
154
- // Go
164
+ // Go with ClangTool
155
165
if (0 == Tool.run (Factory.get ())) {
156
166
iRet = GetTotalError (Memo);
157
167
if (Memo.Config .GetData ()->General .Options .bAllowedPrintResult ) {
@@ -279,12 +289,12 @@ int main(int Argc, const char **Argv) {
279
289
280
290
if (!CheckInputConfig.empty ()) {
281
291
if (!llvm::sys::fs::exists (CheckInputConfig)) {
282
- cout << " ERROR : Failed to find config file." << endl;
292
+ cout << " Error : Failed to find config file." << endl;
283
293
return 2 ;
284
294
}
285
295
string ErrorReason;
286
296
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;
288
298
cout << ErrorReason << endl;
289
299
return 3 ;
290
300
}
@@ -310,6 +320,7 @@ int main(int Argc, const char **Argv) {
310
320
311
321
} else if (TestSubcommand) {
312
322
iRet = RunTest (TestOutputJson, TestNameFilter);
323
+
313
324
} else {
314
325
iRet = -1 ; /* Error (Command miss matched.) */
315
326
cl::PrintHelpMessage ();
0 commit comments