26 #define __COUT_HDR__ ""
31 #define __FILENAME__ \
32 (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
33 #define __MF_SUBJECT__ __FILENAME__
34 #define __MF_DECOR__ (__MF_SUBJECT__)
36 #define __COUT_HDR_L__ ":" << std::dec << __LINE__ << " |\t"
37 #define __COUT_HDR_FL__ __FILENAME__ << "" << __COUT_HDR_L__
38 #define TLOG(X) std::cout << QUOTE(X) << ": " << __COUT_HDR_FL__
39 #define __COUT_ERR__ TLOG(TLVL_ERROR) << __COUT_HDR__
40 #define __COUT_WARN__ TLOG(TLVL_WARN) << __COUT_HDR__
41 #define __COUT_INFO__ TLOG(TLVL_INFO) << __COUT_HDR__
44 TLOG(TLVL_DEBUG) << __COUT_HDR__
47 TLOG(TLVL_TRACE) << __COUT_HDR__
50 std::stringstream ss; \
51 ss << "|" << __MF_DECOR__ << ": " << __COUT_HDR_FL__ << __COUT_HDR__
52 #define __SS_THROW__ \
54 __COUT_ERR__ << "\n" << ss.str(); \
55 throw std::runtime_error(ss.str()); \
57 #define __SS_THROW_ONLY__ \
59 throw std::runtime_error(ss.str()); \
61 #define __E__ std::endl
63 #define __COUTV__(X) __COUT__ << QUOTE(X) << " = " << X << __E__
64 #define __COUTTV__(X) __COUTT__ << QUOTE(X) << " = " << X << __E__
65 #define __COUTVS__(LVL, X) \
66 TLOG(TLVL_DEBUG + LVL) << __COUT_HDR__ << QUOTE(X) << " = " << X << __E__
71 bool isTypeToFix(
const std::string& extension)
73 __COUTTV__(extension);
74 if(extension ==
".cc" ||
75 extension ==
".icc" ||
76 extension ==
".cpp" ||
82 __COUT_INFO__ <<
"===========================> skipping " << extension << __E__;
89 void fixFile(
const std::string& path)
93 std::FILE* fp = std::fopen(path.c_str(),
"rb");
96 __SS__ <<
"Could not open file at " << path <<
". Error: " << errno <<
" - "
97 << strerror(errno) << __E__;
101 std::string newContents =
"";
103 bool isHeaderType = path[path.size()-1] ==
'h';
104 __COUTV__(isHeaderType);
111 bool isNewLine =
true;
112 bool inCommentBlock =
false;
113 bool inHeaderCommentBlock =
false;
114 bool inFunctionBlock =
false;
115 bool skippedLastLine =
false;
117 while(fgets(line,200,fp))
119 size_t len = strlen(line);
125 if(!isNewLine || (len > 10 && line[0] !=
'/' && line[0] !=
'#' &&
126 !(line[0] ==
'\t' && line[1] ==
'/') ))
129 bool inQuote =
false;
130 for(
size_t c = 0; c < len; ++c)
132 newContents += line[c];
134 if(!found && !inQuote && c > 10 && c+2 < len &&
136 line[c+1] ==
'/' && line[c-1] !=
':')
144 newContents +=
"//<";
148 else if(!found && !inQuote && line[c] ==
'"')
150 else if(!found && inQuote && line[c] ==
'"')
154 inHeaderCommentBlock =
false;
155 inCommentBlock =
false;
156 inFunctionBlock =
false;
157 skippedLastLine =
false;
165 if(1 && path.find(
"RootFileExplorer.h") != std::string::npos)
166 __COUT__ << lineNum <<
". " << len <<
": " << inFunctionBlock <<
" " <<
167 inCommentBlock <<
" " <<
172 if(isNewLine && ((len > 2 &&
176 (isHeaderType && len > 3 &&
183 bool headerTab = (isHeaderType && line[0] ==
'\t');
185 if(path.find(
"RootFileExplorer.h") != std::string::npos)
186 __COUT__ << lineNum <<
". " << len <<
": " << inFunctionBlock <<
" " <<
187 inCommentBlock <<
" " << inHeaderCommentBlock <<
" " << headerTab <<
" " <<
190 if(isHeaderType && !inHeaderCommentBlock && !inCommentBlock)
193 std::string testStr(line);
194 size_t i0a = testStr.find(
' ');
195 size_t i0b = testStr.find(
'\t');
196 size_t i1 = testStr.find(
'(');
197 size_t i2 = testStr.find(
')');
198 size_t i3 = testStr.find(
';');
200 if(path.find(
"RootFileExplorer.h") != std::string::npos)
206 if(i0a != std::string::npos || i0b != std::string::npos)
208 size_t i0 = (i0b < i0a)?i0b:i0a;
210 if(path.find(
"RootFileExplorer.h") != std::string::npos)
216 if(i1 != std::string::npos && i2 != std::string::npos && i3 != std::string::npos)
219 if(i0 < i1 && i1 < i2 && i2 < i3)
221 __COUT__ <<
"skipping header commented function delcaration: " << line;
224 skippedLastLine =
true;
229 if(i3 != std::string::npos && i3 == len - 2)
231 __COUT__ <<
"skipping header commented member variable delcaration: " << line;
234 skippedLastLine =
true;
240 if(path.find(
"RootFileExplorer.h") != std::string::npos)
241 __COUT__ << lineNum <<
". " << len <<
": " << inFunctionBlock <<
" " <<
242 inCommentBlock <<
" " << inHeaderCommentBlock <<
" " << headerTab <<
" " <<
246 inFunctionBlock =
true;
247 if(len > 3 && line[2] ==
' ' && line[3] ==
'{')
248 inFunctionBlock =
true;
252 __COUTT__ <<
"skipping comment block: " << line << __E__;
253 inCommentBlock =
false;
258 inFunctionBlock =
false;
259 if(len > 3 && line[2] ==
' ' && line[3] ==
'}')
260 inFunctionBlock =
false;
261 if(len > 3 && line[2] ==
'{' && line[3] ==
'}')
262 inFunctionBlock =
false;
263 if(len > 4 && line[2] ==
'{' && line[4] ==
'}')
264 inFunctionBlock =
false;
265 if(len > 4 && line[3] ==
'{' && line[4] ==
'}')
266 inFunctionBlock =
false;
269 skippedLastLine =
true;
276 std::string testStr(line);
277 if(len < 30 && testStr.find(
"clang-format") != std::string::npos)
279 __COUTT__ <<
"skipping clang: " << line << __E__;
282 skippedLastLine =
true;
285 else if(!inCommentBlock && !inHeaderCommentBlock &&
286 testStr.find(
"#") < 5)
288 __COUTT__ <<
"skipping # pragma: " << line << __E__;
291 skippedLastLine =
true;
294 else if(!inCommentBlock && !inHeaderCommentBlock &&
295 testStr.find(
"BOOST_") < 5)
297 __COUTT__ <<
"skipping BOOST: " << line << __E__;
300 skippedLastLine =
true;
303 else if(!inCommentBlock && !inHeaderCommentBlock &&
304 testStr.find(
"XDAQ_") < 5)
306 __COUTT__ <<
"skipping XDAQ: " << line << __E__;
309 skippedLastLine =
true;
312 else if(!inCommentBlock && !inHeaderCommentBlock &&
313 testStr.find(
"static ") < 5)
315 __COUTT__ <<
"skipping static: " << line << __E__;
318 skippedLastLine =
true;
323 if(0 && path.find(
"JSONDispatcher_module.cc") != std::string::npos)
324 __COUT__ << len <<
": " << inFunctionBlock <<
" " <<
325 inCommentBlock <<
" " << headerTab <<
" " <<
328 if(len == 3 && skippedLastLine)
331 __COUTT__ <<
"skipping gratuitous comment in skipzone: " << line << __E__;
336 else if(headerTab && len == 4 && skippedLastLine)
339 __COUTT__ <<
"skipping gratuitous header comment in skipzone: " << line << __E__;
345 skippedLastLine =
false;
348 if(0 && path.find(
"JSONDispatcher_module.cc") != std::string::npos)
349 __COUT__ << len <<
": " << inFunctionBlock <<
" " <<
350 inCommentBlock <<
" " << headerTab <<
" " <<
353 if(!headerTab && line[2] !=
'/')
355 else if(headerTab && line[3] !=
'/')
357 inHeaderCommentBlock =
true;
358 newContents +=
"\t//";
360 newContents += (&line[3]);
362 if(len && line[len-1] ==
'\n')
366 __COUT__ <<
"No new line at size " << len << __E__;
371 else if(!headerTab && len > 4 && line[2] ==
'/' && line[3] ==
'/' && line[4] ==
'/')
374 __COUTT__ <<
"Ending comment block with /////: " << line << __E__;
375 inCommentBlock =
false;
377 if(len && line[len-1] ==
'\n')
381 __COUT__ <<
"No new line at size " << len << __E__;
388 inHeaderCommentBlock =
true;
390 inCommentBlock =
true;
392 else if(isNewLine && inCommentBlock && len == 1)
394 __COUTT__ <<
"Continue comment block" << __E__;
395 newContents +=
"///";
397 else if(isNewLine && inHeaderCommentBlock && len == 1)
399 __COUTT__ <<
"Continue header comment block" << __E__;
400 newContents +=
"\t///";
404 inHeaderCommentBlock =
false;
405 inCommentBlock =
false;
406 inFunctionBlock =
false;
407 skippedLastLine =
false;
413 if(len && line[len-1] ==
'\n')
417 __COUT__ <<
"No new line at size " << len << __E__;
425 fp = std::fopen(path.c_str(),
"wb");
428 __SS__ <<
"Could not open file to write at " << path <<
". Error: " << errno <<
" - "
429 << strerror(errno) << __E__;
432 std::fwrite(&newContents[0], 1, newContents.size(), fp);
452 void recursiveFixPathContent(
const std::string& path)
457 struct dirent* entry;
462 if(!(pDIR = opendir((path).c_str())))
464 __COUT__ <<
"Path '" << path <<
"' could not be opened!" << __E__;
466 if(isTypeToFix(path.substr(path.rfind(
'.'))))
468 __COUT__ <<
"Interpreting as file: '" << path <<
"'" << __E__;
477 while((entry = readdir(pDIR)))
479 name = std::string(entry->d_name);
480 type = int(entry->d_type);
482 __COUT__ <<
"\t" << type <<
" " << name << std::endl;
493 if(type == 0 || type == 10)
496 DIR* pTmpDIR = opendir((path +
"/" + name).c_str());
503 __COUT__ <<
"Unable to open path as directory: "
504 << (path +
"/" + name) << __E__;
514 __COUT__ <<
"Directory: " << type <<
" " << name << __E__;
516 recursiveFixPathContent(path +
"/" + name);
520 __COUT__ <<
"File: " << type <<
" " << name << std::endl;
524 if(isTypeToFix(name.substr(name.rfind(
'.'))))
526 __COUTT__ <<
"Fixing file: " << name << __E__;
527 fixFile(path +
"/" + name);
532 __COUT_WARN__ <<
"Invalid file extension, skipping '" << name <<
"' ..."
544 int main(
int argc,
char* argv[])
550 "Usage: $0 <path>.\n");
554 std::string path = argv[1];
558 recursiveFixPathContent(path);
560 __COUT_INFO__ <<
"Done." << __E__;
563 catch(
const std::runtime_error& e)
565 __COUT_ERR__ <<
"Error caught during test execution: \n" << e.what() << __E__;