2010-01-06 03:32:54 +08:00
|
|
|
//===- 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 "CIndexer.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/Version.h"
|
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2011-12-23 06:49:47 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2012-12-04 17:25:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-01-06 03:32:54 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <sstream>
|
2012-12-04 17:25:21 +08:00
|
|
|
#include <vector>
|
2010-01-06 03:32:54 +08:00
|
|
|
|
2010-11-09 11:25:16 +08:00
|
|
|
#ifdef __CYGWIN__
|
2011-03-09 06:17:33 +08:00
|
|
|
#include <cygwin/version.h>
|
2010-11-09 11:25:16 +08:00
|
|
|
#include <sys/cygwin.h>
|
|
|
|
#define LLVM_ON_WIN32 1
|
|
|
|
#endif
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2010-10-12 07:17:59 +08:00
|
|
|
std::string CIndexer::getClangResourcesPath() {
|
2010-01-06 03:32:54 +08:00
|
|
|
// Did we already compute the path?
|
2010-10-12 07:17:59 +08:00
|
|
|
if (!ResourcesPath.empty())
|
|
|
|
return ResourcesPath.str();
|
|
|
|
|
|
|
|
// Find the location where this library lives (libclang.dylib).
|
2010-01-06 03:32:54 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
char path[MAX_PATH];
|
|
|
|
VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
|
|
|
|
sizeof(mbi));
|
|
|
|
GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
|
2010-11-09 11:25:16 +08:00
|
|
|
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
char w32path[MAX_PATH];
|
|
|
|
strcpy(w32path, path);
|
2011-03-09 06:17:33 +08:00
|
|
|
#if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
|
|
|
|
cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH);
|
|
|
|
#else
|
2010-11-09 11:25:16 +08:00
|
|
|
cygwin_conv_to_full_posix_path(w32path, path);
|
2011-03-09 06:17:33 +08:00
|
|
|
#endif
|
2010-11-09 11:25:16 +08:00
|
|
|
#endif
|
|
|
|
|
2010-10-12 07:17:59 +08:00
|
|
|
llvm::sys::Path LibClangPath(path);
|
|
|
|
LibClangPath.eraseComponent();
|
2010-01-06 03:32:54 +08:00
|
|
|
#else
|
|
|
|
// This silly cast below avoids a C++ warning.
|
|
|
|
Dl_info info;
|
|
|
|
if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Call to dladdr() failed");
|
2010-10-12 07:17:59 +08:00
|
|
|
|
|
|
|
llvm::sys::Path LibClangPath(info.dli_fname);
|
|
|
|
|
2010-01-06 03:32:54 +08:00
|
|
|
// We now have the CIndex directory, locate clang relative to it.
|
2010-10-12 07:17:59 +08:00
|
|
|
LibClangPath.eraseComponent();
|
2010-01-06 03:32:54 +08:00
|
|
|
#endif
|
2010-10-12 07:17:59 +08:00
|
|
|
|
|
|
|
LibClangPath.appendComponent("clang");
|
|
|
|
LibClangPath.appendComponent(CLANG_VERSION_STRING);
|
2010-01-06 03:32:54 +08:00
|
|
|
|
|
|
|
// Cache our result.
|
2010-10-12 07:17:59 +08:00
|
|
|
ResourcesPath = LibClangPath;
|
|
|
|
return LibClangPath.str();
|
2010-01-06 03:32:54 +08:00
|
|
|
}
|
2010-01-23 08:14:00 +08:00
|
|
|
|
2010-03-14 05:22:49 +08:00
|
|
|
static llvm::sys::Path GetTemporaryPath() {
|
2010-02-02 13:19:57 +08:00
|
|
|
// FIXME: This is lame; sys::Path should provide this function (in particular,
|
|
|
|
// it should know how to find the temporary files dir).
|
|
|
|
std::string Error;
|
|
|
|
const char *TmpDir = ::getenv("TMPDIR");
|
|
|
|
if (!TmpDir)
|
|
|
|
TmpDir = ::getenv("TEMP");
|
|
|
|
if (!TmpDir)
|
|
|
|
TmpDir = ::getenv("TMP");
|
|
|
|
if (!TmpDir)
|
|
|
|
TmpDir = "/tmp";
|
|
|
|
llvm::sys::Path P(TmpDir);
|
2010-03-14 05:22:49 +08:00
|
|
|
P.appendComponent("remap");
|
2010-02-02 13:19:57 +08:00
|
|
|
if (P.makeUnique(false, &Error))
|
|
|
|
return llvm::sys::Path("");
|
|
|
|
|
|
|
|
// FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
|
|
|
|
P.eraseFromDisk(false, 0);
|
|
|
|
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
bool clang::RemapFiles(unsigned num_unsaved_files,
|
|
|
|
struct CXUnsavedFile *unsaved_files,
|
|
|
|
std::vector<std::string> &RemapArgs,
|
|
|
|
std::vector<llvm::sys::Path> &TemporaryFiles) {
|
|
|
|
for (unsigned i = 0; i != num_unsaved_files; ++i) {
|
|
|
|
// Write the contents of this unsaved file into the temporary file.
|
2010-03-14 05:22:49 +08:00
|
|
|
llvm::sys::Path SavedFile(GetTemporaryPath());
|
2010-02-02 13:19:57 +08:00
|
|
|
if (SavedFile.empty())
|
|
|
|
return true;
|
|
|
|
|
2010-01-23 08:14:00 +08:00
|
|
|
std::string ErrorInfo;
|
2010-10-12 09:01:43 +08:00
|
|
|
llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo,
|
|
|
|
llvm::raw_fd_ostream::F_Binary);
|
2010-01-23 08:14:00 +08:00
|
|
|
if (!ErrorInfo.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
OS.write(unsaved_files[i].Contents, unsaved_files[i].Length);
|
|
|
|
OS.close();
|
|
|
|
if (OS.has_error()) {
|
|
|
|
SavedFile.eraseFromDisk();
|
2010-05-28 04:16:37 +08:00
|
|
|
OS.clear_error();
|
2010-01-23 08:14:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the file.
|
|
|
|
std::string RemapArg = unsaved_files[i].Filename;
|
|
|
|
RemapArg += ';';
|
2010-02-02 13:19:57 +08:00
|
|
|
RemapArg += SavedFile.str();
|
2010-01-23 08:14:00 +08:00
|
|
|
RemapArgs.push_back("-Xclang");
|
|
|
|
RemapArgs.push_back("-remap-file");
|
|
|
|
RemapArgs.push_back("-Xclang");
|
|
|
|
RemapArgs.push_back(RemapArg);
|
|
|
|
TemporaryFiles.push_back(SavedFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|