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" |
| 24 | #include "llvm/Config/config.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/System/Program.h" |
| 29 | |
| 30 | #include <cstdio> |
| 31 | #include <vector> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #ifdef LLVM_ON_WIN32 |
| 35 | #include <windows.h> |
| 36 | #else |
| 37 | #include <dlfcn.h> |
| 38 | #endif |
| 39 | |
| 40 | using namespace clang; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 41 | |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 42 | std::string CIndexer::getClangResourcesPath() { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 43 | // Did we already compute the path? |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 44 | if (!ResourcesPath.empty()) |
| 45 | return ResourcesPath.str(); |
| 46 | |
| 47 | // Find the location where this library lives (libclang.dylib). |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 48 | #ifdef LLVM_ON_WIN32 |
| 49 | MEMORY_BASIC_INFORMATION mbi; |
| 50 | char path[MAX_PATH]; |
| 51 | VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi, |
| 52 | sizeof(mbi)); |
| 53 | GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH); |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 54 | |
| 55 | llvm::sys::Path LibClangPath(path); |
| 56 | LibClangPath.eraseComponent(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 57 | #else |
| 58 | // This silly cast below avoids a C++ warning. |
| 59 | Dl_info info; |
| 60 | if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0) |
| 61 | assert(0 && "Call to dladdr() failed"); |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 62 | |
| 63 | llvm::sys::Path LibClangPath(info.dli_fname); |
| 64 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 65 | // We now have the CIndex directory, locate clang relative to it. |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 66 | LibClangPath.eraseComponent(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 67 | #endif |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 68 | |
| 69 | LibClangPath.appendComponent("clang"); |
| 70 | LibClangPath.appendComponent(CLANG_VERSION_STRING); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 71 | |
| 72 | // Cache our result. |
Douglas Gregor | d1e6fdb | 2010-10-11 23:17:59 +0000 | [diff] [blame^] | 73 | ResourcesPath = LibClangPath; |
| 74 | return LibClangPath.str(); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 75 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 76 | |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 77 | static llvm::sys::Path GetTemporaryPath() { |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 78 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 79 | // it should know how to find the temporary files dir). |
| 80 | std::string Error; |
| 81 | const char *TmpDir = ::getenv("TMPDIR"); |
| 82 | if (!TmpDir) |
| 83 | TmpDir = ::getenv("TEMP"); |
| 84 | if (!TmpDir) |
| 85 | TmpDir = ::getenv("TMP"); |
| 86 | if (!TmpDir) |
| 87 | TmpDir = "/tmp"; |
| 88 | llvm::sys::Path P(TmpDir); |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 89 | P.appendComponent("remap"); |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 90 | if (P.makeUnique(false, &Error)) |
| 91 | return llvm::sys::Path(""); |
| 92 | |
| 93 | // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837. |
| 94 | P.eraseFromDisk(false, 0); |
| 95 | |
| 96 | return P; |
| 97 | } |
| 98 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 99 | bool clang::RemapFiles(unsigned num_unsaved_files, |
| 100 | struct CXUnsavedFile *unsaved_files, |
| 101 | std::vector<std::string> &RemapArgs, |
| 102 | std::vector<llvm::sys::Path> &TemporaryFiles) { |
| 103 | for (unsigned i = 0; i != num_unsaved_files; ++i) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 104 | // Write the contents of this unsaved file into the temporary file. |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame] | 105 | llvm::sys::Path SavedFile(GetTemporaryPath()); |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 106 | if (SavedFile.empty()) |
| 107 | return true; |
| 108 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 109 | std::string ErrorInfo; |
| 110 | llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo); |
| 111 | if (!ErrorInfo.empty()) |
| 112 | return true; |
| 113 | |
| 114 | OS.write(unsaved_files[i].Contents, unsaved_files[i].Length); |
| 115 | OS.close(); |
| 116 | if (OS.has_error()) { |
| 117 | SavedFile.eraseFromDisk(); |
Dan Gohman | ebaf257 | 2010-05-27 20:16:37 +0000 | [diff] [blame] | 118 | OS.clear_error(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | // Remap the file. |
| 123 | std::string RemapArg = unsaved_files[i].Filename; |
| 124 | RemapArg += ';'; |
Daniel Dunbar | 74198af | 2010-02-02 05:19:57 +0000 | [diff] [blame] | 125 | RemapArg += SavedFile.str(); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 126 | RemapArgs.push_back("-Xclang"); |
| 127 | RemapArgs.push_back("-remap-file"); |
| 128 | RemapArgs.push_back("-Xclang"); |
| 129 | RemapArgs.push_back(RemapArg); |
| 130 | TemporaryFiles.push_back(SavedFile); |
| 131 | } |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |