Refactor: Move file extension checks from Tokenizer to Path class. This has also functional change as now also file.JAVA is considered a Java file.
This commit is contained in:
parent
8378153669
commit
e91d239ea5
64
lib/path.cpp
64
lib/path.cpp
|
@ -108,6 +108,13 @@ bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This wrapper exists because Sun's CC does not allow a static_cast
|
||||||
|
// from extern "C" int(*)(int) to int(*)(int).
|
||||||
|
static int tolowerWrapper(int c)
|
||||||
|
{
|
||||||
|
return std::tolower(c);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Path::removeQuotationMarks(std::string path)
|
std::string Path::removeQuotationMarks(std::string path)
|
||||||
{
|
{
|
||||||
path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
|
path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
|
||||||
|
@ -124,26 +131,29 @@ std::string Path::getFilenameExtension(const std::string &path)
|
||||||
return extension;
|
return extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
|
||||||
// This wrapper exists because Sun's CC does not allow a static_cast
|
|
||||||
// from extern "C" int(*)(int) to int(*)(int).
|
|
||||||
static int tolowerWrapper(int c)
|
|
||||||
{
|
{
|
||||||
return std::tolower(c);
|
std::string extension = getFilenameExtension(path);
|
||||||
|
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
|
||||||
|
return extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Path::isC(const std::string &path)
|
||||||
bool Path::acceptFile(const std::string &filename)
|
|
||||||
{
|
{
|
||||||
std::string extension = Path::getFilenameExtension(filename);
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
||||||
if (extension == "")
|
if (extension == ".c") {
|
||||||
return false;
|
return true;
|
||||||
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Path::isCPP(const std::string &path)
|
||||||
|
{
|
||||||
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
||||||
if (extension == ".cpp" ||
|
if (extension == ".cpp" ||
|
||||||
extension == ".cxx" ||
|
extension == ".cxx" ||
|
||||||
extension == ".cc" ||
|
extension == ".cc" ||
|
||||||
extension == ".c" ||
|
|
||||||
extension == ".c++" ||
|
extension == ".c++" ||
|
||||||
extension == ".tpp" ||
|
extension == ".tpp" ||
|
||||||
extension == ".txx") {
|
extension == ".txx") {
|
||||||
|
@ -153,3 +163,33 @@ bool Path::acceptFile(const std::string &filename)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Path::isJava(const std::string &path)
|
||||||
|
{
|
||||||
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
||||||
|
if (extension == ".java") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Path::isCSharp(const std::string &path)
|
||||||
|
{
|
||||||
|
const std::string extension = getFilenameExtensionInLowerCase(path);
|
||||||
|
if (extension == ".cs") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Path::acceptFile(const std::string &filename)
|
||||||
|
{
|
||||||
|
if (Path::isCPP(filename) ||
|
||||||
|
Path::isC(filename)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
36
lib/path.h
36
lib/path.h
|
@ -73,10 +73,17 @@ public:
|
||||||
/**
|
/**
|
||||||
* @brief Get an extension of the filename.
|
* @brief Get an extension of the filename.
|
||||||
* @param path Path containing filename.
|
* @param path Path containing filename.
|
||||||
* @return Filename extension (containing the dot, e.g. ".h").
|
* @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
|
||||||
*/
|
*/
|
||||||
static std::string getFilenameExtension(const std::string &path);
|
static std::string getFilenameExtension(const std::string &path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get an extension of the filename in lower case.
|
||||||
|
* @param path Path containing filename.
|
||||||
|
* @return Filename extension (containing the dot, e.g. ".h").
|
||||||
|
*/
|
||||||
|
static std::string getFilenameExtensionInLowerCase(const std::string &path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the file extension indicates that it's a C/C++ source file.
|
* @brief Check if the file extension indicates that it's a C/C++ source file.
|
||||||
* Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
|
* Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
|
||||||
|
@ -85,6 +92,33 @@ public:
|
||||||
*/
|
*/
|
||||||
static bool acceptFile(const std::string &filename);
|
static bool acceptFile(const std::string &filename);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Identify language based on file extension.
|
||||||
|
* @param extensionInLowerCase e.g. ".c"
|
||||||
|
* @return true if extension is meant for C files
|
||||||
|
*/
|
||||||
|
static bool isC(const std::string &extensionInLowerCase);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Identify language based on file extension.
|
||||||
|
* @param extensionInLowerCase e.g. ".cpp"
|
||||||
|
* @return true if extension is meant for C++ files
|
||||||
|
*/
|
||||||
|
static bool isCPP(const std::string &extensionInLowerCase);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Identify language based on file extension.
|
||||||
|
* @param extensionInLowerCase e.g. ".java"
|
||||||
|
* @return true if extension is meant for Java files
|
||||||
|
*/
|
||||||
|
static bool isJava(const std::string &extensionInLowerCase);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Identify language based on file extension.
|
||||||
|
* @param extensionInLowerCase e.g. ".cs"
|
||||||
|
* @return true if extension is meant for C# files
|
||||||
|
*/
|
||||||
|
static bool isCSharp(const std::string &extensionInLowerCase);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -9503,3 +9503,29 @@ void Tokenizer::printUnknownTypes()
|
||||||
_errorLogger->reportOut(ss.str());
|
_errorLogger->reportOut(ss.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Tokenizer::getSourceFilePath() const {
|
||||||
|
if (_files.empty())
|
||||||
|
return std::string("");
|
||||||
|
return _files[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isJava() const {
|
||||||
|
return Path::isJava(getSourceFilePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isCSharp() const {
|
||||||
|
return Path::isCSharp(getSourceFilePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isJavaOrCSharp() const {
|
||||||
|
return isJava() || isCSharp();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isC() const {
|
||||||
|
return Path::isC(getSourceFilePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Tokenizer::isCPP() const {
|
||||||
|
return Path::isCPP(getSourceFilePath());
|
||||||
|
}
|
||||||
|
|
|
@ -49,38 +49,23 @@ public:
|
||||||
Tokenizer(const Settings * settings, ErrorLogger *errorLogger);
|
Tokenizer(const Settings * settings, ErrorLogger *errorLogger);
|
||||||
virtual ~Tokenizer();
|
virtual ~Tokenizer();
|
||||||
|
|
||||||
/** The file extension. Used by isC() etc. */
|
/** Returns the source file path. e.g. "file.cpp" */
|
||||||
std::string fileExtension() const {
|
std::string getSourceFilePath() const;
|
||||||
if (_files.empty())
|
|
||||||
return std::string("");
|
|
||||||
return Path::getFilenameExtension(_files[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Is the code JAVA. Used for bailouts */
|
/** Is the code JAVA. Used for bailouts */
|
||||||
bool isJava() const {
|
bool isJava() const;
|
||||||
return fileExtension() == ".java";
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Is the code C#. Used for bailouts */
|
/** Is the code C#. Used for bailouts */
|
||||||
bool isCSharp() const {
|
bool isCSharp() const;
|
||||||
return fileExtension() == ".cs";
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Is the code JAVA/C#. Used for bailouts */
|
/** Is the code JAVA/C#. Used for bailouts */
|
||||||
bool isJavaOrCSharp() const {
|
bool isJavaOrCSharp() const;
|
||||||
return isJava() || isCSharp();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Is the code C. Used for bailouts */
|
/** Is the code C. Used for bailouts */
|
||||||
bool isC() const {
|
bool isC() const;
|
||||||
const std::string ext = fileExtension();
|
|
||||||
return (ext == ".c" || ext == ".C");
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Is the code CPP. Used for bailouts */
|
/** Is the code CPP. Used for bailouts */
|
||||||
bool isCPP() const {
|
bool isCPP() const;
|
||||||
return !isC() && (_files.size() && Path::acceptFile(_files[0]));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if inner scope ends with a call to a noreturn function
|
* Check if inner scope ends with a call to a noreturn function
|
||||||
|
|
|
@ -29,6 +29,11 @@ private:
|
||||||
|
|
||||||
void run() {
|
void run() {
|
||||||
TEST_CASE(simplify_path);
|
TEST_CASE(simplify_path);
|
||||||
|
TEST_CASE(accept_file);
|
||||||
|
TEST_CASE(is_c);
|
||||||
|
TEST_CASE(is_cpp);
|
||||||
|
TEST_CASE(is_java);
|
||||||
|
TEST_CASE(is_csharp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplify_path() {
|
void simplify_path() {
|
||||||
|
@ -58,6 +63,48 @@ private:
|
||||||
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("the/\"path to\"/index.cpp"));
|
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("the/\"path to\"/index.cpp"));
|
||||||
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("\"the/path to/index.cpp\""));
|
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("\"the/path to/index.cpp\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void accept_file() {
|
||||||
|
ASSERT(Path::acceptFile("index.cpp"));
|
||||||
|
ASSERT(Path::acceptFile("index.invalid.cpp"));
|
||||||
|
ASSERT(Path::acceptFile("index.invalid.Cpp"));
|
||||||
|
ASSERT(Path::acceptFile("index.invalid.C"));
|
||||||
|
ASSERT(Path::acceptFile("index.invalid.C++"));
|
||||||
|
ASSERT(Path::acceptFile("index.")==false);
|
||||||
|
ASSERT(Path::acceptFile("index")==false);
|
||||||
|
ASSERT(Path::acceptFile("")==false);
|
||||||
|
ASSERT(Path::acceptFile("C")==false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void is_c() {
|
||||||
|
ASSERT(Path::isC("index.cpp")==false);
|
||||||
|
ASSERT(Path::isC("")==false);
|
||||||
|
ASSERT(Path::isC("c")==false);
|
||||||
|
ASSERT(Path::isC("index.c"));
|
||||||
|
ASSERT(Path::isC("C:\\foo\\index.c"));
|
||||||
|
ASSERT(Path::isC("C:\\foo\\index.C"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void is_cpp() {
|
||||||
|
ASSERT(Path::isCPP("index.c")==false);
|
||||||
|
ASSERT(Path::isCPP("index.cpp"));
|
||||||
|
ASSERT(Path::isCPP("C:\\foo\\index.cpp"));
|
||||||
|
ASSERT(Path::isCPP("C:\\foo\\index.Cpp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void is_java() {
|
||||||
|
ASSERT(Path::isJava("index.cpp")==false);
|
||||||
|
ASSERT(Path::isJava("index.java"));
|
||||||
|
ASSERT(Path::isJava("C:\\foo\\index.java"));
|
||||||
|
ASSERT(Path::isJava("C:\\foo\\index.Java"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void is_csharp() {
|
||||||
|
ASSERT(Path::isCSharp("index.cpp")==false);
|
||||||
|
ASSERT(Path::isCSharp("index.cs"));
|
||||||
|
ASSERT(Path::isCSharp("C:\\foo\\index.cs"));
|
||||||
|
ASSERT(Path::isCSharp("C:\\foo\\index.Cs"));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestPath)
|
REGISTER_TEST(TestPath)
|
||||||
|
|
Loading…
Reference in New Issue