Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 1 | //===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Clang-C Source Indexing library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CIndexer.h" |
| 15 | |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/Version.h" |
| 22 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
Dylan Noblesmith | 1770e0d | 2011-12-22 22:49:47 +0000 | [diff] [blame] | 24 | #include "llvm/Config/llvm-config.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Program.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 29 | |
| 30 | #include <cstdio> |
| 31 | #include <vector> |
| 32 | #include <sstream> |
| 33 | |
NAKAMURA Takumi | 3b35a4d | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 34 | #ifdef __CYGWIN__ |
NAKAMURA Takumi | 893793b | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 35 | #include <cygwin/version.h> |
NAKAMURA Takumi | 3b35a4d | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 36 | #include <sys/cygwin.h> |
| 37 | #define LLVM_ON_WIN32 1 |
| 38 | #endif |
| 39 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 40 | #ifdef LLVM_ON_WIN32 |
| 41 | #include <windows.h> |
| 42 | #else |
| 43 | #include <dlfcn.h> |
| 44 | #endif |
| 45 | |
| 46 | using namespace clang; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 48 | std::string CIndexer::getClangResourcesPath() { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 49 | // Did we already compute the path? |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 50 | if (!ResourcesPath.empty()) |
| 51 | return ResourcesPath.str(); |
| 52 | |
| 53 | // Find the location where this library lives (libclang.dylib). |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 54 | #ifdef LLVM_ON_WIN32 |
| 55 | MEMORY_BASIC_INFORMATION mbi; |
| 56 | char path[MAX_PATH]; |
| 57 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
| 58 | sizeof(mbi)); |
| 59 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
NAKAMURA Takumi | 3b35a4d | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef __CYGWIN__ |
| 62 | char w32path[MAX_PATH]; |
| 63 | strcpy(w32path, path); |
NAKAMURA Takumi | 893793b | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 64 | #if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181 |
| 65 | cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH); |
| 66 | #else |
NAKAMURA Takumi | 3b35a4d | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 67 | cygwin_conv_to_full_posix_path(w32path, path); |
| 68 | #endif |
NAKAMURA Takumi | 893793b | 2011-03-08 22:17:33 +0000 | [diff] [blame] | 69 | #endif |
NAKAMURA Takumi | 3b35a4d | 2010-11-09 03:25:16 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 71 | llvm::sys::Path LibClangPath(path); |
| 72 | LibClangPath.eraseComponent(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 73 | #else |
| 74 | // This silly cast below avoids a C++ warning. |
| 75 | Dl_info info; |
| 76 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 77 | llvm_unreachable("Call to dladdr() failed"); |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 78 | |
| 79 | llvm::sys::Path LibClangPath(info.dli_fname); |
| 80 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 81 | // We now have the CIndex directory, locate clang relative to it. |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 82 | LibClangPath.eraseComponent(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 83 | #endif |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 84 | |
| 85 | LibClangPath.appendComponent("clang"); |
| 86 | LibClangPath.appendComponent(CLANG_VERSION_STRING); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 87 | |
| 88 | // Cache our result. |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame] | 89 | ResourcesPath = LibClangPath; |
| 90 | return LibClangPath.str(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 91 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 92 | |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 93 | static llvm::sys::Path GetTemporaryPath() { |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 94 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 95 | // it should know how to find the temporary files dir). |
| 96 | std::string Error; |
| 97 | const char *TmpDir = ::getenv("TMPDIR"); |
| 98 | if (!TmpDir) |
| 99 | TmpDir = ::getenv("TEMP"); |
| 100 | if (!TmpDir) |
| 101 | TmpDir = ::getenv("TMP"); |
| 102 | if (!TmpDir) |
| 103 | TmpDir = "/tmp"; |
| 104 | llvm::sys::Path P(TmpDir); |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 105 | P.appendComponent("remap"); |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 106 | if (P.makeUnique(false, &Error)) |
| 107 | return llvm::sys::Path(""); |
| 108 | |
| 109 | // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837. |
| 110 | P.eraseFromDisk(false, 0); |
| 111 | |
| 112 | return P; |
| 113 | } |
| 114 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 115 | bool clang::RemapFiles(unsigned num_unsaved_files, |
| 116 | struct CXUnsavedFile *unsaved_files, |
| 117 | std::vector<std::string> &RemapArgs, |
| 118 | std::vector<llvm::sys::Path> &TemporaryFiles) { |
| 119 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 120 | // Write the contents of this unsaved file into the temporary file. |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 121 | llvm::sys::Path SavedFile(GetTemporaryPath()); |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 122 | if (SavedFile.empty()) |
| 123 | return true; |
| 124 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 125 | std::string ErrorInfo; |
Francois Pichet | c44fe4b | 2010-10-12 01:01:43 +0000 | [diff] [blame] | 126 | llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo, |
| 127 | llvm::raw_fd_ostream::F_Binary); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 128 | if (!ErrorInfo.empty()) |
| 129 | return true; |
| 130 | |
| 131 | OS.write(unsaved_files[i].Contents, unsaved_files[i].Length); |
| 132 | OS.close(); |
| 133 | if (OS.has_error()) { |
| 134 | SavedFile.eraseFromDisk(); |
Dan Gohman | ebaf257 | 2010-05-27 20:16:37 +0000 | [diff] [blame] | 135 | OS.clear_error(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // Remap the file. |
| 140 | std::string RemapArg = unsaved_files[i].Filename; |
| 141 | RemapArg += ';'; |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 142 | RemapArg += SavedFile.str(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 143 | RemapArgs.push_back("-Xclang"); |
| 144 | RemapArgs.push_back("-remap-file"); |
| 145 | RemapArgs.push_back("-Xclang"); |
| 146 | RemapArgs.push_back(RemapArg); |
| 147 | TemporaryFiles.push_back(SavedFile); |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |