blob: b328d5cd22b940e64900f3975642bb89e95200c1 [file] [log] [blame]
Ted Kremenekab188932010-01-05 19:32:54 +00001//===- 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
40using namespace clang;
Ted Kremenekab188932010-01-05 19:32:54 +000041
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000042std::string CIndexer::getClangResourcesPath() {
Ted Kremenekab188932010-01-05 19:32:54 +000043 // Did we already compute the path?
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000044 if (!ResourcesPath.empty())
45 return ResourcesPath.str();
46
47 // Find the location where this library lives (libclang.dylib).
Ted Kremenekab188932010-01-05 19:32:54 +000048#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 Gregord1e6fdb2010-10-11 23:17:59 +000054
55 llvm::sys::Path LibClangPath(path);
56 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000057#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 Gregord1e6fdb2010-10-11 23:17:59 +000062
63 llvm::sys::Path LibClangPath(info.dli_fname);
64
Ted Kremenekab188932010-01-05 19:32:54 +000065 // We now have the CIndex directory, locate clang relative to it.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000066 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000067#endif
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000068
69 LibClangPath.appendComponent("clang");
70 LibClangPath.appendComponent(CLANG_VERSION_STRING);
Ted Kremenekab188932010-01-05 19:32:54 +000071
72 // Cache our result.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000073 ResourcesPath = LibClangPath;
74 return LibClangPath.str();
Ted Kremenekab188932010-01-05 19:32:54 +000075}
Douglas Gregor4db64a42010-01-23 00:14:00 +000076
Benjamin Kramerc2a98162010-03-13 21:22:49 +000077static llvm::sys::Path GetTemporaryPath() {
Daniel Dunbar74198af2010-02-02 05:19:57 +000078 // 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 Kramerc2a98162010-03-13 21:22:49 +000089 P.appendComponent("remap");
Daniel Dunbar74198af2010-02-02 05:19:57 +000090 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 Gregor4db64a42010-01-23 00:14:00 +000099bool 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 Gregor4db64a42010-01-23 00:14:00 +0000104 // Write the contents of this unsaved file into the temporary file.
Benjamin Kramerc2a98162010-03-13 21:22:49 +0000105 llvm::sys::Path SavedFile(GetTemporaryPath());
Daniel Dunbar74198af2010-02-02 05:19:57 +0000106 if (SavedFile.empty())
107 return true;
108
Douglas Gregor4db64a42010-01-23 00:14:00 +0000109 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 Gohmanebaf2572010-05-27 20:16:37 +0000118 OS.clear_error();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 return true;
120 }
121
122 // Remap the file.
123 std::string RemapArg = unsaved_files[i].Filename;
124 RemapArg += ';';
Daniel Dunbar74198af2010-02-02 05:19:57 +0000125 RemapArg += SavedFile.str();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000126 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