Skip to content

Commit e91d239

Browse files
committed
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.
1 parent 8378153 commit e91d239

File tree

5 files changed

+167
-35
lines changed

5 files changed

+167
-35
lines changed

lib/path.cpp

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
108108
#endif
109109
}
110110

111+
// This wrapper exists because Sun's CC does not allow a static_cast
112+
// from extern "C" int(*)(int) to int(*)(int).
113+
static int tolowerWrapper(int c)
114+
{
115+
return std::tolower(c);
116+
}
117+
111118
std::string Path::removeQuotationMarks(std::string path)
112119
{
113120
path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
@@ -124,26 +131,29 @@ std::string Path::getFilenameExtension(const std::string &path)
124131
return extension;
125132
}
126133

127-
128-
// This wrapper exists because Sun's CC does not allow a static_cast
129-
// from extern "C" int(*)(int) to int(*)(int).
130-
static int tolowerWrapper(int c)
134+
std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
131135
{
132-
return std::tolower(c);
136+
std::string extension = getFilenameExtension(path);
137+
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
138+
return extension;
133139
}
134140

135-
136-
bool Path::acceptFile(const std::string &filename)
141+
bool Path::isC(const std::string &path)
137142
{
138-
std::string extension = Path::getFilenameExtension(filename);
139-
if (extension == "")
140-
return false;
141-
std::transform(extension.begin(), extension.end(), extension.begin(), tolowerWrapper);
143+
const std::string extension = getFilenameExtensionInLowerCase(path);
144+
if (extension == ".c") {
145+
return true;
146+
}
142147

148+
return false;
149+
}
150+
151+
bool Path::isCPP(const std::string &path)
152+
{
153+
const std::string extension = getFilenameExtensionInLowerCase(path);
143154
if (extension == ".cpp" ||
144155
extension == ".cxx" ||
145156
extension == ".cc" ||
146-
extension == ".c" ||
147157
extension == ".c++" ||
148158
extension == ".tpp" ||
149159
extension == ".txx") {
@@ -153,3 +163,33 @@ bool Path::acceptFile(const std::string &filename)
153163
return false;
154164
}
155165

166+
bool Path::isJava(const std::string &path)
167+
{
168+
const std::string extension = getFilenameExtensionInLowerCase(path);
169+
if (extension == ".java") {
170+
return true;
171+
}
172+
173+
return false;
174+
}
175+
176+
bool Path::isCSharp(const std::string &path)
177+
{
178+
const std::string extension = getFilenameExtensionInLowerCase(path);
179+
if (extension == ".cs") {
180+
return true;
181+
}
182+
183+
return false;
184+
}
185+
186+
bool Path::acceptFile(const std::string &filename)
187+
{
188+
if (Path::isCPP(filename) ||
189+
Path::isC(filename)) {
190+
return true;
191+
}
192+
193+
return false;
194+
}
195+

lib/path.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ class Path {
7373
/**
7474
* @brief Get an extension of the filename.
7575
* @param path Path containing filename.
76-
* @return Filename extension (containing the dot, e.g. ".h").
76+
* @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
7777
*/
7878
static std::string getFilenameExtension(const std::string &path);
7979

80+
/**
81+
* @brief Get an extension of the filename in lower case.
82+
* @param path Path containing filename.
83+
* @return Filename extension (containing the dot, e.g. ".h").
84+
*/
85+
static std::string getFilenameExtensionInLowerCase(const std::string &path);
86+
8087
/**
8188
* @brief Check if the file extension indicates that it's a C/C++ source file.
8289
* Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
@@ -85,6 +92,33 @@ class Path {
8592
*/
8693
static bool acceptFile(const std::string &filename);
8794

95+
/**
96+
* @brief Identify language based on file extension.
97+
* @param extensionInLowerCase e.g. ".c"
98+
* @return true if extension is meant for C files
99+
*/
100+
static bool isC(const std::string &extensionInLowerCase);
101+
102+
/**
103+
* @brief Identify language based on file extension.
104+
* @param extensionInLowerCase e.g. ".cpp"
105+
* @return true if extension is meant for C++ files
106+
*/
107+
static bool isCPP(const std::string &extensionInLowerCase);
108+
109+
/**
110+
* @brief Identify language based on file extension.
111+
* @param extensionInLowerCase e.g. ".java"
112+
* @return true if extension is meant for Java files
113+
*/
114+
static bool isJava(const std::string &extensionInLowerCase);
115+
116+
/**
117+
* @brief Identify language based on file extension.
118+
* @param extensionInLowerCase e.g. ".cs"
119+
* @return true if extension is meant for C# files
120+
*/
121+
static bool isCSharp(const std::string &extensionInLowerCase);
88122
};
89123

90124
/// @}

lib/tokenize.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9503,3 +9503,29 @@ void Tokenizer::printUnknownTypes()
95039503
_errorLogger->reportOut(ss.str());
95049504
}
95059505
}
9506+
9507+
std::string Tokenizer::getSourceFilePath() const {
9508+
if (_files.empty())
9509+
return std::string("");
9510+
return _files[0];
9511+
}
9512+
9513+
bool Tokenizer::isJava() const {
9514+
return Path::isJava(getSourceFilePath());
9515+
}
9516+
9517+
bool Tokenizer::isCSharp() const {
9518+
return Path::isCSharp(getSourceFilePath());
9519+
}
9520+
9521+
bool Tokenizer::isJavaOrCSharp() const {
9522+
return isJava() || isCSharp();
9523+
}
9524+
9525+
bool Tokenizer::isC() const {
9526+
return Path::isC(getSourceFilePath());
9527+
}
9528+
9529+
bool Tokenizer::isCPP() const {
9530+
return Path::isCPP(getSourceFilePath());
9531+
}

lib/tokenize.h

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,38 +49,23 @@ class Tokenizer {
4949
Tokenizer(const Settings * settings, ErrorLogger *errorLogger);
5050
virtual ~Tokenizer();
5151

52-
/** The file extension. Used by isC() etc. */
53-
std::string fileExtension() const {
54-
if (_files.empty())
55-
return std::string("");
56-
return Path::getFilenameExtension(_files[0]);
57-
}
52+
/** Returns the source file path. e.g. "file.cpp" */
53+
std::string getSourceFilePath() const;
5854

5955
/** Is the code JAVA. Used for bailouts */
60-
bool isJava() const {
61-
return fileExtension() == ".java";
62-
}
56+
bool isJava() const;
6357

6458
/** Is the code C#. Used for bailouts */
65-
bool isCSharp() const {
66-
return fileExtension() == ".cs";
67-
}
59+
bool isCSharp() const;
6860

6961
/** Is the code JAVA/C#. Used for bailouts */
70-
bool isJavaOrCSharp() const {
71-
return isJava() || isCSharp();
72-
}
62+
bool isJavaOrCSharp() const;
7363

7464
/** Is the code C. Used for bailouts */
75-
bool isC() const {
76-
const std::string ext = fileExtension();
77-
return (ext == ".c" || ext == ".C");
78-
}
65+
bool isC() const;
7966

8067
/** Is the code CPP. Used for bailouts */
81-
bool isCPP() const {
82-
return !isC() && (_files.size() && Path::acceptFile(_files[0]));
83-
}
68+
bool isCPP() const;
8469

8570
/**
8671
* Check if inner scope ends with a call to a noreturn function

test/testpath.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class TestPath : public TestFixture {
2929

3030
void run() {
3131
TEST_CASE(simplify_path);
32+
TEST_CASE(accept_file);
33+
TEST_CASE(is_c);
34+
TEST_CASE(is_cpp);
35+
TEST_CASE(is_java);
36+
TEST_CASE(is_csharp);
3237
}
3338

3439
void simplify_path() {
@@ -58,6 +63,48 @@ class TestPath : public TestFixture {
5863
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("the/\"path to\"/index.cpp"));
5964
ASSERT_EQUALS("the/path to/index.cpp", Path::removeQuotationMarks("\"the/path to/index.cpp\""));
6065
}
66+
67+
void accept_file() {
68+
ASSERT(Path::acceptFile("index.cpp"));
69+
ASSERT(Path::acceptFile("index.invalid.cpp"));
70+
ASSERT(Path::acceptFile("index.invalid.Cpp"));
71+
ASSERT(Path::acceptFile("index.invalid.C"));
72+
ASSERT(Path::acceptFile("index.invalid.C++"));
73+
ASSERT(Path::acceptFile("index.")==false);
74+
ASSERT(Path::acceptFile("index")==false);
75+
ASSERT(Path::acceptFile("")==false);
76+
ASSERT(Path::acceptFile("C")==false);
77+
}
78+
79+
void is_c() {
80+
ASSERT(Path::isC("index.cpp")==false);
81+
ASSERT(Path::isC("")==false);
82+
ASSERT(Path::isC("c")==false);
83+
ASSERT(Path::isC("index.c"));
84+
ASSERT(Path::isC("C:\\foo\\index.c"));
85+
ASSERT(Path::isC("C:\\foo\\index.C"));
86+
}
87+
88+
void is_cpp() {
89+
ASSERT(Path::isCPP("index.c")==false);
90+
ASSERT(Path::isCPP("index.cpp"));
91+
ASSERT(Path::isCPP("C:\\foo\\index.cpp"));
92+
ASSERT(Path::isCPP("C:\\foo\\index.Cpp"));
93+
}
94+
95+
void is_java() {
96+
ASSERT(Path::isJava("index.cpp")==false);
97+
ASSERT(Path::isJava("index.java"));
98+
ASSERT(Path::isJava("C:\\foo\\index.java"));
99+
ASSERT(Path::isJava("C:\\foo\\index.Java"));
100+
}
101+
102+
void is_csharp() {
103+
ASSERT(Path::isCSharp("index.cpp")==false);
104+
ASSERT(Path::isCSharp("index.cs"));
105+
ASSERT(Path::isCSharp("C:\\foo\\index.cs"));
106+
ASSERT(Path::isCSharp("C:\\foo\\index.Cs"));
107+
}
61108
};
62109

63110
REGISTER_TEST(TestPath)

0 commit comments

Comments
 (0)