forked from qt-creator/qt-creator
Moved CheckUndefinedSymbols in libCPlusPlus
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include <cplusplus/pp.h>
|
||||
#include <cplusplus/CppBindings.h>
|
||||
#include <cplusplus/Overview.h>
|
||||
#include <cplusplus/CheckUndefinedSymbols.h>
|
||||
|
||||
#include "cppmodelmanager.h"
|
||||
#include "cpptoolsconstants.h"
|
||||
@@ -253,351 +254,6 @@ void CppPreprocessor::setTodo(const QStringList &files)
|
||||
|
||||
namespace {
|
||||
|
||||
class Process;
|
||||
|
||||
class CheckUndefinedSymbols: protected ASTVisitor
|
||||
{
|
||||
QSet<QByteArray> _types;
|
||||
QSet<QByteArray> _namespaceNames;
|
||||
|
||||
public:
|
||||
CheckUndefinedSymbols(Document::Ptr doc)
|
||||
: ASTVisitor(doc->control()), _process(0), _doc(doc)
|
||||
{ }
|
||||
|
||||
void setGlobalNamespaceBinding(NamespaceBindingPtr globalNamespaceBinding)
|
||||
{
|
||||
_globalNamespaceBinding = globalNamespaceBinding;
|
||||
_types.clear();
|
||||
|
||||
if (_globalNamespaceBinding) {
|
||||
QSet<NamespaceBinding *> processed;
|
||||
buildTypeMap(_globalNamespaceBinding.data(), &processed);
|
||||
}
|
||||
}
|
||||
|
||||
void operator()(AST *ast, Process *process)
|
||||
{ _process = process; accept(ast); }
|
||||
|
||||
protected:
|
||||
using ASTVisitor::visit;
|
||||
|
||||
void addType(Name *name)
|
||||
{
|
||||
if (! name)
|
||||
return;
|
||||
|
||||
if (Identifier *id = name->identifier())
|
||||
_types.insert(QByteArray(id->chars(), id->size()));
|
||||
}
|
||||
|
||||
void buildTypeMap(Class *klass)
|
||||
{
|
||||
addType(klass->name());
|
||||
|
||||
for (unsigned i = 0; i < klass->memberCount(); ++i) {
|
||||
Symbol *member = klass->memberAt(i);
|
||||
|
||||
if (Class *klass = member->asClass()) {
|
||||
buildTypeMap(klass);
|
||||
} else if (Enum *e = member->asEnum()) {
|
||||
addType(e->name());
|
||||
} else if (ForwardClassDeclaration *fwd = member->asForwardClassDeclaration()) {
|
||||
addType(fwd->name());
|
||||
} else if (Declaration *decl = member->asDeclaration()) {
|
||||
if (decl->isTypedef())
|
||||
addType(decl->name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void buildTypeMap(NamespaceBinding *binding, QSet<NamespaceBinding *> *processed)
|
||||
{
|
||||
if (! processed->contains(binding)) {
|
||||
processed->insert(binding);
|
||||
|
||||
if (Identifier *id = binding->identifier()) {
|
||||
_namespaceNames.insert(QByteArray(id->chars(), id->size()));
|
||||
}
|
||||
|
||||
foreach (Namespace *ns, binding->symbols) {
|
||||
for (unsigned i = 0; i < ns->memberCount(); ++i) {
|
||||
Symbol *member = ns->memberAt(i);
|
||||
|
||||
if (Class *klass = member->asClass()) {
|
||||
buildTypeMap(klass);
|
||||
} else if (Enum *e = member->asEnum()) {
|
||||
addType(e->name());
|
||||
} else if (ForwardClassDeclaration *fwd = member->asForwardClassDeclaration()) {
|
||||
addType(fwd->name());
|
||||
} else if (Declaration *decl = member->asDeclaration()) {
|
||||
if (decl->isTypedef())
|
||||
addType(decl->name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (NamespaceBinding *childBinding, binding->children) {
|
||||
buildTypeMap(childBinding, processed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QList<FunctionDeclaratorAST *> functionDeclarationStack;
|
||||
|
||||
FunctionDeclaratorAST *currentFunctionDeclarator() const
|
||||
{
|
||||
if (functionDeclarationStack.isEmpty())
|
||||
return 0;
|
||||
|
||||
return functionDeclarationStack.last();
|
||||
}
|
||||
|
||||
virtual bool visit(FunctionDeclaratorAST *ast)
|
||||
{
|
||||
functionDeclarationStack.append(ast);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void endVisit(FunctionDeclaratorAST *)
|
||||
{
|
||||
functionDeclarationStack.removeLast();
|
||||
}
|
||||
|
||||
virtual bool visit(TypeofSpecifierAST *ast)
|
||||
{
|
||||
accept(ast->next);
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual bool visit(TypenameTypeParameterAST *ast)
|
||||
{
|
||||
if (NameAST *nameAst = ast->name)
|
||||
addType(nameAst->name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(TemplateTypeParameterAST *ast)
|
||||
{
|
||||
if (ast->name)
|
||||
addType(ast->name->name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(NamedTypeSpecifierAST *ast)
|
||||
{
|
||||
if (ast->name) {
|
||||
if (! ast->name->name) {
|
||||
unsigned line, col;
|
||||
getTokenStartPosition(ast->firstToken(), &line, &col);
|
||||
// qWarning() << _doc->fileName() << line << col;
|
||||
} else if (Identifier *id = ast->name->name->identifier()) {
|
||||
if (! _types.contains(QByteArray::fromRawData(id->chars(), id->size()))) {
|
||||
if (FunctionDeclaratorAST *functionDeclarator = currentFunctionDeclarator()) {
|
||||
if (functionDeclarator->as_cpp_initializer)
|
||||
return true;
|
||||
}
|
||||
|
||||
Overview oo;
|
||||
translationUnit()->warning(ast->firstToken(), "`%s' is not a type name",
|
||||
qPrintable(oo(ast->name->name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(ClassSpecifierAST *ast)
|
||||
{
|
||||
if (ast->base_clause) {
|
||||
unsigned line, col;
|
||||
getTokenStartPosition(ast->firstToken(), &line, &col);
|
||||
_context = lookupContext(line, col);
|
||||
}
|
||||
|
||||
bool hasQ_OBJECT_CHECK = false;
|
||||
|
||||
if (ast->symbol) {
|
||||
Class *klass = ast->symbol->asClass();
|
||||
|
||||
for (unsigned i = 0; i < klass->memberCount(); ++i) {
|
||||
Symbol *symbol = klass->memberAt(i);
|
||||
|
||||
if (symbol->name() && symbol->name()->isNameId()) {
|
||||
NameId *nameId = symbol->name()->asNameId();
|
||||
|
||||
if (! qstrcmp(nameId->identifier()->chars(), "qt_check_for_QOBJECT_macro")) {
|
||||
hasQ_OBJECT_CHECK = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_qobjectStack.append(hasQ_OBJECT_CHECK);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void endVisit(ClassSpecifierAST *)
|
||||
{ _qobjectStack.removeLast(); }
|
||||
|
||||
bool qobjectCheck() const
|
||||
{
|
||||
if (_qobjectStack.isEmpty())
|
||||
return false;
|
||||
|
||||
return _qobjectStack.last();
|
||||
}
|
||||
|
||||
virtual bool visit(FunctionDefinitionAST *ast)
|
||||
{
|
||||
if (ast->symbol) {
|
||||
Function *fun = ast->symbol->asFunction();
|
||||
if ((fun->isSignal() || fun->isSlot()) && ! qobjectCheck()) {
|
||||
translationUnit()->warning(ast->firstToken(),
|
||||
"you forgot the Q_OBJECT macro");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(SimpleDeclarationAST *ast)
|
||||
{
|
||||
const bool check = qobjectCheck();
|
||||
for (List<Declaration *> *it = ast->symbols; it; it = it->next) {
|
||||
Declaration *decl = it->value;
|
||||
|
||||
if (Function *fun = decl->type()->asFunctionType()) {
|
||||
if ((fun->isSignal() || fun->isSlot()) && ! check) {
|
||||
translationUnit()->warning(ast->firstToken(),
|
||||
"you forgot the Q_OBJECT macro");
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(BaseSpecifierAST *base)
|
||||
{
|
||||
if (base->name) {
|
||||
const QList<Symbol *> symbols = _context.resolveClass(base->name->name);
|
||||
|
||||
if (symbols.isEmpty()) {
|
||||
const char *token = "after `:'";
|
||||
|
||||
if (base->comma_token)
|
||||
token = "after `,'";
|
||||
|
||||
translationUnit()->warning(base->name->firstToken(),
|
||||
"expected class-name %s token", token);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(UsingDirectiveAST *ast)
|
||||
{
|
||||
if (ast->symbol && ast->symbol->name() && _globalNamespaceBinding) {
|
||||
const Location loc = Location(ast->symbol);
|
||||
|
||||
NamespaceBinding *binding = _globalNamespaceBinding.data();
|
||||
|
||||
if (Scope *enclosingNamespaceScope = ast->symbol->enclosingNamespaceScope())
|
||||
binding = NamespaceBinding::find(enclosingNamespaceScope->owner()->asNamespace(), binding);
|
||||
|
||||
if (! binding || ! binding->resolveNamespace(loc, ast->symbol->name())) {
|
||||
translationUnit()->warning(ast->name->firstToken(),
|
||||
"expected a namespace");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(QualifiedNameAST *ast)
|
||||
{
|
||||
if (ast->name) {
|
||||
QualifiedNameId *q = ast->name->asQualifiedNameId();
|
||||
for (unsigned i = 0; i < q->nameCount() - 1; ++i) {
|
||||
Name *name = q->nameAt(i);
|
||||
if (Identifier *id = name->identifier()) {
|
||||
const QByteArray spell = QByteArray::fromRawData(id->chars(), id->size());
|
||||
if (! (_namespaceNames.contains(spell) || _types.contains(spell))) {
|
||||
translationUnit()->warning(ast->firstToken(),
|
||||
"`%s' is not a namespace or class name",
|
||||
spell.constData());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(CastExpressionAST *ast)
|
||||
{
|
||||
if (ast->lparen_token && ast->type_id && ast->rparen_token && ast->expression) {
|
||||
if (TypeIdAST *cast_type_id = ast->type_id->asTypeId()) {
|
||||
SpecifierAST *type_specifier = cast_type_id->type_specifier;
|
||||
if (! cast_type_id->declarator && type_specifier && ! type_specifier->next &&
|
||||
type_specifier->asNamedTypeSpecifier() && ast->expression &&
|
||||
ast->expression->asUnaryExpression()) {
|
||||
// this ast node is ambigious, e.g.
|
||||
// (a) + b
|
||||
// it can be parsed as
|
||||
// ((a) + b)
|
||||
// or
|
||||
// (a) (+b)
|
||||
accept(ast->expression);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual bool visit(SizeofExpressionAST *ast)
|
||||
{
|
||||
if (ast->lparen_token && ast->expression && ast->rparen_token) {
|
||||
if (TypeIdAST *type_id = ast->expression->asTypeId()) {
|
||||
SpecifierAST *type_specifier = type_id->type_specifier;
|
||||
if (! type_id->declarator && type_specifier && ! type_specifier->next &&
|
||||
type_specifier->asNamedTypeSpecifier()) {
|
||||
// this sizeof expression is ambiguos, e.g.
|
||||
// sizeof (a)
|
||||
// `a' can be a typeid or a nested-expression.
|
||||
return false;
|
||||
} else if (type_id->declarator
|
||||
&& type_id->declarator->postfix_declarators
|
||||
&& ! type_id->declarator->postfix_declarators->next
|
||||
&& type_id->declarator->postfix_declarators->asArrayDeclarator() != 0) {
|
||||
// this sizeof expression is ambiguos, e.g.
|
||||
// sizeof(a[10])
|
||||
// `a' can be a typeid or an expression.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LookupContext lookupContext(unsigned line, unsigned column) const;
|
||||
|
||||
private:
|
||||
Process *_process;
|
||||
Document::Ptr _doc;
|
||||
LookupContext _context;
|
||||
NamespaceBindingPtr _globalNamespaceBinding;
|
||||
QList<bool> _qobjectStack;
|
||||
};
|
||||
|
||||
class Process: public std::unary_function<Document::Ptr, void>
|
||||
{
|
||||
QPointer<CppModelManager> _modelManager;
|
||||
@@ -643,7 +299,7 @@ public:
|
||||
CheckUndefinedSymbols checkUndefinedSymbols(doc);
|
||||
checkUndefinedSymbols.setGlobalNamespaceBinding(ns);
|
||||
|
||||
checkUndefinedSymbols(doc->translationUnit()->ast(), this);
|
||||
checkUndefinedSymbols(doc->translationUnit()->ast()); // ### FIXME
|
||||
}
|
||||
|
||||
doc->releaseTranslationUnit();
|
||||
@@ -653,9 +309,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
LookupContext CheckUndefinedSymbols::lookupContext(unsigned line, unsigned column) const
|
||||
{ return _process->lookupContext(line, column); }
|
||||
|
||||
} // end of anonymous namespace
|
||||
|
||||
void CppPreprocessor::run(const QString &fileName)
|
||||
|
Reference in New Issue
Block a user