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(); _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) void TokenList::deleteTokens(Token *tok)
{ {
while (tok) { while (tok) {
@ -236,24 +248,9 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
// Extract the filename // Extract the filename
line = line.substr(1, line.length() - 2); line = line.substr(1, line.length() - 2);
// Has this file been tokenized already?
++lineno; ++lineno;
bool foundOurfile = false;
fileIndexes.push(FileIndex); fileIndexes.push(FileIndex);
for (unsigned int i = 0; i < _files.size(); ++i) { FileIndex = appendFileIfNew(line);
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);
}
lineNumbers.push(lineno); lineNumbers.push(lineno);
lineno = 0; lineno = 0;
} else { } else {
@ -297,12 +294,22 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0)
std::getline(code, line); std::getline(code, line);
// Update the current line number
unsigned int row; unsigned int row;
if (!(std::stringstream(line) >> row)) std::istringstream fiss(line);
++lineno; if (fiss >> row) {
else // Update the current line number
lineno = row; 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(); CurrentToken.clear();
continue; continue;
} else if (CurrentToken == "#endfile") { } else if (CurrentToken == "#endfile") {

View File

@ -65,6 +65,9 @@ public:
/** Deallocate list */ /** Deallocate list */
void deallocateTokens(); 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 */ /** get first token of list */
const Token *front() const { const Token *front() const {
return _front; return _front;

View File

@ -319,6 +319,7 @@ private:
TEST_CASE(file3); TEST_CASE(file3);
TEST_CASE(line1); // Ticket #4408 TEST_CASE(line1); // Ticket #4408
TEST_CASE(line2); // Ticket #5423
TEST_CASE(doublesharp); 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() { void doublesharp() {