Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,16 @@ namespace clangimport {
std::string nodeType;
std::vector<AstNodePtr> children;

bool isPrologueTypedefDecl() const;
void setLocations(TokenList &tokenList, int file, int line, int col);

void dumpAst(int num = 0, int indent = 0) const;
void createTokens1(TokenList &tokenList) {
//dumpAst(); // TODO: reactivate or remove
if (isPrologueTypedefDecl())
return;
if (!tokenList.back()) {
setLocations(tokenList, 0, 1, 1);
// FIXME: treat as C++ if no filename (i.e. no lang) is specified for now
if (tokenList.getSourceFilePath().empty())
tokenList.setLang(Standards::Language::CPP);
}
else
setLocations(tokenList, tokenList.back()->fileIndex(), tokenList.back()->linenr(), 1);
Expand Down Expand Up @@ -502,9 +502,43 @@ void clangimport::AstNode::dumpAst(int num, int indent) const
}
}

bool clangimport::AstNode::isPrologueTypedefDecl() const
{
// these TypedefDecl are included in *any* AST dump and we should ignore them as they should not be of interest to us
// see https://github.com/llvm/llvm-project/issues/120228#issuecomment-2549212109 for an explanation
if (nodeType != TypedefDecl)
return false;

// TODO: use different values to indicate "<invalid sloc>"?
if (mFile != 0 || mLine != 1 || mCol != 1)
return false;

// TODO: match without using children
if (children.empty())
return false;

if (children[0].get()->mExtTokens.size() < 2)
return false;

const auto& type = children[0].get()->mExtTokens[1];
if (type == "'__int128'" ||
type == "'unsigned __int128'" ||
type == "'struct __NSConstantString_tag'" ||
type == "'char *'" ||
type == "'struct __va_list_tag[1]'")
{
// NOLINTNEXTLINE(readability-simplify-boolean-expr)
return true;
}

return false;
}

void clangimport::AstNode::setLocations(TokenList &tokenList, int file, int line, int col)
{
for (const std::string &ext: mExtTokens) {
if (mExtTokens.size() >= 2)
{
const std::string &ext = mExtTokens[1];
if (startsWith(ext, "<col:"))
col = strToInt<int>(ext.substr(5, ext.find_first_of(",>", 5) - 5));
else if (startsWith(ext, "<line:")) {
Expand All @@ -521,6 +555,12 @@ void clangimport::AstNode::setLocations(TokenList &tokenList, int file, int line
file = tokenList.appendFileIfNew(ext.substr(1, sep1 - 1));
line = strToInt<int>(ext.substr(sep1 + 1, sep2 - sep1 - 1));
}
else {
// "<invalid sloc>" are encountered in every AST dump by some built-in TypedefDecl
// an completely empty location block was encountered with a CompoundStmt
if (ext != "<<invalid sloc>" && ext != "<>")
throw InternalError(nullptr, "invalid AST location: " + ext, InternalError::AST);
}
}
}
mFile = file;
Expand Down
Loading