//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Clang-C Source Indexing library.
//
//===----------------------------------------------------------------------===//
#include "clang-c/Index.h"
#include "clang/Index/Program.h"
#include "clang/Index/Indexer.h"
#include "clang/Index/ASTLocation.h"
#include "clang/Index/Utils.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/ASTUnit.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/System/Path.h"
#include "llvm/System/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdio>
#include <vector>
#include <sstream>
#ifdef LLVM_ON_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <dlfcn.h>
#endif
using namespace clang;
using namespace idx;
namespace {
static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
{
NamedDecl *D = DRE->getDecl();
if (isa<VarDecl>(D))
return CXCursor_VarRef;
else if (isa<FunctionDecl>(D))
return CXCursor_FunctionRef;
else if (isa<EnumConstantDecl>(D))
return CXCursor_EnumConstantRef;
else
return CXCursor_NotImplemented;
}
#if 0
// Will be useful one day.
class CRefVisitor : public StmtVisitor<CRefVisitor> {
CXDecl CDecl;
CXDeclIterator Callback;
CXClientData CData;
void Call(enum CXCursorKind CK, Stmt *SRef) {
CXCursor C = { CK, CDecl, SRef };
Callback(CDecl, C, CData);
}
public:
CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
CDecl(C), Callback(cback), CData(D) {}
void VisitStmt(Stmt *S) {
for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
C != CEnd; ++C)
Visit(*C);
}
void VisitDeclRefExpr(DeclRefExpr *Node) {
Call(TranslateDeclRefExpr(Node), Node);
}
void VisitMemberExpr(MemberExpr *Node) {
Call(CXCursor_MemberRef, Node);
}
void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
Call(CXCursor_ObjCSelectorRef, Node);
}
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
Call(CXCursor_ObjCIvarRef, Node);
}
};
#endif
/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
/// warnings and errors.
class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
public:
virtual ~IgnoreDiagnosticsClient() {}
virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
};
// Translation Unit Visitor.
class TUVisitor : public DeclVisitor<TUVisitor> {
CXTranslationUnit TUnit;
CXTranslationUnitIterator Callback;
CXClientData CData;
// MaxPCHLevel - the maximum PCH level of declarations that we will pass on
// to the visitor. Declarations with a PCH level greater than this value will
// be suppressed.
unsigned MaxPCHLevel;
void Call(enum CXCursorKind CK, NamedDecl *ND) {
// Filter any decla