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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* Script to move ITK_DISALLOW_COPY_AND_MOVE calls to the public section
* of the classes.
*
* Initial version by Niels Dekker, LKEB, Leiden University Medical Center, 2018
* Discourse discussion: https://discourse.itk.org/t/noncopyable/648/24
* compiling:
* g++ Move_DISALLOW_COPY_to_public_section.cpp -lstdc++fs
* clang++ Move_DISALLOW_COPY_to_public_section.cpp -lstdc++fs
* Tested 29/03/2018 with:
* g++ --version : 7.3.1
* clang++ --version : 7.0.0 */
#include <cassert>
#include <cctype>
#include <deque>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstring>
#include <string>
using namespace std::filesystem;
namespace
{
using Lines = std::deque<std::string>;
auto
ReadFile(const path & filePath)
{
Lines result;
std::ifstream inputFileStream{ filePath };
std::string line;
while (std::getline(inputFileStream, line))
{
result.push_back(line);
}
return result;
}
void
WriteFile(const path & filePath, const Lines & lines)
{
std::ofstream outputFileStream{ filePath };
for (const auto & line : lines)
{
outputFileStream << line << '\n';
}
}
std::string
ExtractClassName(std::string candidateClassName)
{
for (auto & c : candidateClassName)
{
if (c == ':')
{
c = '\0';
return candidateClassName.c_str();
}
if (c == '<')
{
c = '\0';
return candidateClassName.c_str();
}
if (!std::isalnum(c))
{
return {};
}
}
return candidateClassName;
}
std::string
FindClassName(const char * const line)
{
std::istringstream inputStringStream{ line };
std::string candidateClassName;
inputStringStream >> candidateClassName;
const char exportPostfix[] = "_EXPORT";
if (candidateClassName.size() >= sizeof(exportPostfix) &&
candidateClassName.compare(
candidateClassName.size() + 1 - sizeof(exportPostfix), sizeof(exportPostfix) - 1, exportPostfix) == 0)
{
inputStringStream >> candidateClassName;
}
return ExtractClassName(candidateClassName);
}
const char *
GoToFirstNonSpace(const char * ptr)
{
while (*ptr == ' ')
{
++ptr;
}
return ptr;
}
template <unsigned int VLength>
bool
StringStartsWithPrefix(const char *& str, const char (&prefix)[VLength])
{
assert(prefix[VLength - 1] == '\0');
if ((std::strlen(str) + 1 >= VLength) && (std::memcmp(str, prefix, VLength - 1) == 0))
{
// Move the 'str' pointer beyond the prefix.
str += VLength - 1;
return true;
}
return false;
}
struct Statistics
{
unsigned int numberOfClassNameMismatches;
unsigned int numberOfDisallowMacroCalls;
unsigned int numberOfMissingPublicSections;
unsigned int numberOfSuccessfullMoves;
unsigned int numberOfDisallowMacroCallsAlreadyAtRightPlace;
};
Statistics
ModifyLines(Lines & lines)
{
Statistics statistics = {};
auto className = std::string{};
auto publicLineNumber = Lines::size_type{};
for (auto lineNumber = Lines::size_type{}; lineNumber < lines.size(); ++lineNumber)
{
const auto & line = lines[lineNumber];
const auto numberOfChars = line.size();
if (numberOfChars > 0)
{
const char * c_str = GoToFirstNonSpace(line.c_str());
if (StringStartsWithPrefix(c_str, "class") && (*c_str == ' '))
{
const auto newClassName = FindClassName(c_str);
if (!newClassName.empty())
{
className = newClassName;
publicLineNumber = 0;
}
}
else
{
if ((publicLineNumber == 0) && (!className.empty()) && StringStartsWithPrefix(c_str, "public"))
{
if ((*GoToFirstNonSpace(c_str) == ':') && (*GoToFirstNonSpace(c_str + 1) == '\0'))
{
// Found the first 'public' section of the class named 'className'.
publicLineNumber = lineNumber;
}
}
else
{
if (StringStartsWithPrefix(c_str, "ITK_DISALLOW_COPY_AND_MOVE(") ||
StringStartsWithPrefix(c_str, "ITK_DISALLOW_COPY_AND_ASSIGN("))
{
++statistics.numberOfDisallowMacroCalls;
if (publicLineNumber > 0)
{
assert(!className.empty());
// Found a DISALLOW macro call, somewhere after a 'public' section of the class named 'className'.
const char c = *GoToFirstNonSpace(c_str);
if (c_str == (className + ");"))
{
if (lineNumber == publicLineNumber + 1)
{
std::cout << "Macro call already at the right place: " << lines[lineNumber] << std::endl;
++statistics.numberOfDisallowMacroCallsAlreadyAtRightPlace;
}
else
{
{
// Move the DISALLOW macro call up to the begin of the 'public' section.
std::string disallowMacroCall = std::move(lines[lineNumber]);
for (auto i = lineNumber; i > publicLineNumber + 1; --i)
{
lines[i] = std::move(lines[i - 1]);
}
lines[publicLineNumber + 1] = std::move(disallowMacroCall);
}
if ((lines.size() > (lineNumber + 1)) && lines[lineNumber + 1].empty())
{
// Ensure that there is no empty line left below the original DISALLOW macro call location.
lines.erase(lines.begin() + lineNumber + 1);
}
if ((lines.size() > (lineNumber + 1)) && *GoToFirstNonSpace(lines[lineNumber + 1].c_str()) == '}')
{
auto ptr = GoToFirstNonSpace(lines[lineNumber].c_str());
if (StringStartsWithPrefix(ptr, "private"))
{
if ((*GoToFirstNonSpace(ptr) == ':') && (*GoToFirstNonSpace(ptr + 1) == '\0'))
{
// Erase empty private section.
lines.erase(lines.begin() + lineNumber);
if (lines[lineNumber - 1].empty())
{
// Erase empty line before the erased private section.
lines.erase(lines.begin() + lineNumber - 1);
}
}
}
}
if (!lines[publicLineNumber + 2].empty())
{
// Ensure that there is an empty line below the new DISALLOW macro call location.
lines.insert(lines.begin() + publicLineNumber + 2, std::string{});
}
// Reset local variables for the next iteration.
className = std::string{};
publicLineNumber = Lines::size_type{};
++statistics.numberOfSuccessfullMoves;
}
}
else
{
++statistics.numberOfClassNameMismatches;
std::cerr << "Mismatch! Class name: \"" << className << "\"; macro call: " << lines[lineNumber]
<< std::endl;
}
}
else
{
++statistics.numberOfMissingPublicSections;
std::cerr << "No public section found for macro call " << lines[lineNumber] << std::endl;
}
}
}
}
}
}
return statistics;
}
auto
ProcessFile(const path & filePath)
{
auto lines = ReadFile(filePath);
const auto statistics = ModifyLines(lines);
if (statistics.numberOfSuccessfullMoves > 0)
{
WriteFile(filePath, lines);
}
return statistics;
}
void
ProcessDirectory(const path & directoryPath)
{
Statistics statistics = {};
const recursive_directory_iterator end;
unsigned int numberOfModifiedFiles = 0;
for (recursive_directory_iterator it{ directoryPath }; it != end; ++it)
{
const auto & path = it->path();
const auto & extension = path.extension();
if ((!extension.empty()) && (extension.string() == ".h" || extension.string() == ".cxx") && is_regular_file(path))
{
const auto statisticsPerFile = ProcessFile(path);
if (statisticsPerFile.numberOfDisallowMacroCalls > 0)
{
numberOfModifiedFiles += (statisticsPerFile.numberOfSuccessfullMoves > 0) ? 1 : 0;
if ((statisticsPerFile.numberOfDisallowMacroCalls > 1) ||
(statisticsPerFile.numberOfDisallowMacroCalls != statisticsPerFile.numberOfSuccessfullMoves))
std::cout << statisticsPerFile.numberOfDisallowMacroCalls << ' ' << statisticsPerFile.numberOfSuccessfullMoves
<< ' ' << statisticsPerFile.numberOfDisallowMacroCallsAlreadyAtRightPlace << ' '
<< statisticsPerFile.numberOfClassNameMismatches << ' '
<< statisticsPerFile.numberOfMissingPublicSections << ' ' << path << std::endl;
}
statistics.numberOfDisallowMacroCalls += statisticsPerFile.numberOfDisallowMacroCalls;
statistics.numberOfSuccessfullMoves += statisticsPerFile.numberOfSuccessfullMoves;
statistics.numberOfDisallowMacroCallsAlreadyAtRightPlace +=
statisticsPerFile.numberOfDisallowMacroCallsAlreadyAtRightPlace;
statistics.numberOfClassNameMismatches += statisticsPerFile.numberOfClassNameMismatches;
statistics.numberOfMissingPublicSections += statisticsPerFile.numberOfMissingPublicSections;
}
}
std::cout << "numberOfModifiedFiles:\t" << numberOfModifiedFiles << "\nDisallowMacroCalls:\t"
<< statistics.numberOfDisallowMacroCalls << "\nSuccessfullMoves:\t" << statistics.numberOfSuccessfullMoves
<< "\nDisallowMacroCallsAlreadyAtRightPlace:\t" << statistics.numberOfDisallowMacroCallsAlreadyAtRightPlace
<< "\nClassNameMismatches:\t" << statistics.numberOfClassNameMismatches << "\nMissingPublicSections:\t"
<< statistics.numberOfMissingPublicSections << std::endl;
}
} // namespace
int
main(int argc, char ** argv)
{
if (argc != 2)
{
std::cout << "Please specify the source directory path as command-line argument."
"\nNote: This program will modify the source files in-place!!!"
<< std::endl;
}
else
{
if (argv == nullptr)
{
return EXIT_FAILURE;
}
const char * const arg = argv[1];
if (arg == nullptr)
{
return EXIT_FAILURE;
}
ProcessDirectory(arg);
}
std::cout << "Press anything to continue" << std::endl;
std::cin.get();
return EXIT_SUCCESS;
}
|