blob: 992d76a2efe9d0d7d97fbb0f79940191684c4b83 [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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000028#include "llvm/Support/Program.h"
Ted Kremenekab188932010-01-05 19:32:54 +000029
30#include <cstdio>
31#include <vector>
32#include <sstream>
33
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000034#ifdef __CYGWIN__
35#include <sys/cygwin.h>
36#define LLVM_ON_WIN32 1
37#endif
38
Ted Kremenekab188932010-01-05 19:32:54 +000039#ifdef LLVM_ON_WIN32
40#include <windows.h>
41#else
42#include <dlfcn.h>
43#endif
44
45using namespace clang;
Ted Kremenekab188932010-01-05 19:32:54 +000046
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000047std::string CIndexer::getClangResourcesPath() {
Ted Kremenekab188932010-01-05 19:32:54 +000048 // Did we already compute the path?
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000049 if (!ResourcesPath.empty())
50 return ResourcesPath.str();
51
52 // Find the location where this library lives (libclang.dylib).
Ted Kremenekab188932010-01-05 19:32:54 +000053#ifdef LLVM_ON_WIN32
54 MEMORY_BASIC_INFORMATION mbi;
55 char path[MAX_PATH];
56 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
57 sizeof(mbi));
58 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000059
60#ifdef __CYGWIN__
61 char w32path[MAX_PATH];
62 strcpy(w32path, path);
63 cygwin_conv_to_full_posix_path(w32path, path);
64#endif
65
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000066 llvm::sys::Path LibClangPath(path);
67 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000068#else
69 // This silly cast below avoids a C++ warning.
70 Dl_info info;
71 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
72 assert(0 && "Call to dladdr() failed");
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000073
74 llvm::sys::Path LibClangPath(info.dli_fname);
75
Ted Kremenekab188932010-01-05 19:32:54 +000076 // We now have the CIndex directory, locate clang relative to it.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000077 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000078#endif
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000079
80 LibClangPath.appendComponent("clang");
81 LibClangPath.appendComponent(CLANG_VERSION_STRING);
Ted Kremenekab188932010-01-05 19:32:54 +000082
83 // Cache our result.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000084 ResourcesPath = LibClangPath;
85 return LibClangPath.str();
Ted Kremenekab188932010-01-05 19:32:54 +000086}
Douglas Gregor4db64a42010-01-23 00:14:00 +000087
Benjamin Kramerc2a98162010-03-13 21:22:49 +000088static llvm::sys::Path GetTemporaryPath() {
Daniel Dunbar74198af2010-02-02 05:19:57 +000089 // FIXME: This is lame; sys::Path should provide this function (in particular,
90 // it should know how to find the temporary files dir).
91 std::string Error;
92 const char *TmpDir = ::getenv("TMPDIR");
93 if (!TmpDir)
94 TmpDir = ::getenv("TEMP");
95 if (!TmpDir)
96 TmpDir = ::getenv("TMP");
97 if (!TmpDir)
98 TmpDir = "/tmp";
99 llvm::sys::Path P(TmpDir);
Benjamin Kramerc2a98162010-03-13 21:22:49 +0000100 P.appendComponent("remap");
Daniel Dunbar74198af2010-02-02 05:19:57 +0000101 if (P.makeUnique(false, &Error))
102 return llvm::sys::Path("");
103
104 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
105 P.eraseFromDisk(false, 0);
106
107 return P;
108}
109
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110bool clang::RemapFiles(unsigned num_unsaved_files,
111 struct CXUnsavedFile *unsaved_files,
112 std::vector<std::string> &RemapArgs,
113 std::vector<llvm::sys::Path> &TemporaryFiles) {
114 for (unsigned i = 0; i != num_unsaved_files; ++i) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 // Write the contents of this unsaved file into the temporary file.
Benjamin Kramerc2a98162010-03-13 21:22:49 +0000116 llvm::sys::Path SavedFile(GetTemporaryPath());
Daniel Dunbar74198af2010-02-02 05:19:57 +0000117 if (SavedFile.empty())
118 return true;
119
Douglas Gregor4db64a42010-01-23 00:14:00 +0000120 std::string ErrorInfo;
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000121 llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo,
122 llvm::raw_fd_ostream::F_Binary);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000123 if (!ErrorInfo.empty())
124 return true;
125
126 OS.write(unsaved_files[i].Contents, unsaved_files[i].Length);
127 OS.close();
128 if (OS.has_error()) {
129 SavedFile.eraseFromDisk();
Dan Gohmanebaf2572010-05-27 20:16:37 +0000130 OS.clear_error();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000131 return true;
132 }
133
134 // Remap the file.
135 std::string RemapArg = unsaved_files[i].Filename;
136 RemapArg += ';';
Daniel Dunbar74198af2010-02-02 05:19:57 +0000137 RemapArg += SavedFile.str();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000138 RemapArgs.push_back("-Xclang");
139 RemapArgs.push_back("-remap-file");
140 RemapArgs.push_back("-Xclang");
141 RemapArgs.push_back(RemapArg);
142 TemporaryFiles.push_back(SavedFile);
143 }
144
145 return false;
146}
147