Fixed #5423 (fix parsing of #line NNNN "file.c")

This commit is contained in:
Kamil Dudka 2014-01-28 17:15:07 +01:00 committed by Daniel Marjamäki
parent abe8439917
commit dee18d10a8
3 changed files with 47 additions and 20 deletions

View File

@ -54,6 +54,18 @@ void TokenList::deallocateTokens()
_files.clear();
}
unsigned int TokenList::appendFileIfNew(const std::string &fileName)
{
// Has this file been tokenized already?
for (unsigned int i = 0; i < _files.size(); ++i)
if (Path::sameFileName(_files[i], fileName))
return i;
// The "_files" vector remembers what files have been tokenized..
_files.push_back(Path::simplifyPath(fileName.c_str()));
return static_cast<unsigned int>(_files.size() - 1);
}
void TokenList::deleteTokens(Token *tok)
{
while (tok) {
@ -236,24 +248,9 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
// Extract the filename
line = line.substr(1, line.length() - 2);
// Has this file been tokenized already?
++lineno;
bool foundOurfile = false;
fileIndexes.push(FileIndex);
for (unsigned int i = 0; i < _files.size(); ++i) {
if (Path::sameFileName(_files[i], line)) {
// Use this index
foundOurfile = true;
FileIndex = i;
}
}
if (!foundOurfile) {
// The "_files" vector remembers what files have been tokenized..
_files.push_back(Path::simplifyPath(line.c_str()));
FileIndex = static_cast<unsigned int>(_files.size() - 1);
}
FileIndex = appendFileIfNew(line);
lineNumbers.push(lineno);
lineno = 0;
} else {
@ -297,12 +294,22 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
std::getline(code, line);
// Update the current line number
unsigned int row;
if (!(std::stringstream(line) >> row))
++lineno;
else
std::istringstream fiss(line);
if (fiss >> row) {
// Update the current line number
lineno = row;
std::string line;
if (std::getline(fiss, line) && line.length() > 4U) {
// _"file_name" -> file_name
line = line.substr(2, line.length() - 3);
// Update the current file
FileIndex = appendFileIfNew(line);
}
} else
++lineno;
CurrentToken.clear();
continue;
} else if (CurrentToken == "#endfile") {

View File

@ -65,6 +65,9 @@ public:
/** Deallocate list */
void deallocateTokens();
/** append file name if seen the first time; return its index in any case */
unsigned int appendFileIfNew(const std::string &file);
/** get first token of list */
const Token *front() const {
return _front;

View File

@ -319,6 +319,7 @@ private:
TEST_CASE(file3);
TEST_CASE(line1); // Ticket #4408
TEST_CASE(line2); // Ticket #5423
TEST_CASE(doublesharp);
@ -5159,6 +5160,22 @@ private:
}
}
void line2() {
const char code[] = "#line 8 \"c:\\a.h\"\n"
"123\n";
errout.str("");
Settings settings;
// tokenize..
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "a.cpp");
ASSERT_EQUALS(Path::toNativeSeparators("[c:\\a.h:8]"), tokenizer.list.fileLine(tokenizer.tokens()));
}
void doublesharp() {