blob: d89e0a41984d5b5881d5d7a78973d2dfab1532fe [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"
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclVisitor.h"
17#include "clang/AST/StmtVisitor.h"
18#include "clang/Basic/FileManager.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Basic/Version.h"
21#include "clang/Sema/CodeCompleteConsumer.h"
22#include "llvm/ADT/StringExtras.h"
Dylan Noblesmith1770e0d2011-12-22 22:49:47 +000023#include "llvm/Config/llvm-config.h"
Ted Kremenekab188932010-01-05 19:32:54 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000026#include "llvm/Support/Program.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenekab188932010-01-05 19:32:54 +000028#include <cstdio>
Ted Kremenekab188932010-01-05 19:32:54 +000029#include <sstream>
Chandler Carruthf59edb92012-12-04 09:25:21 +000030#include <vector>
Ted Kremenekab188932010-01-05 19:32:54 +000031
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000032#ifdef __CYGWIN__
NAKAMURA Takumi893793b2011-03-08 22:17:33 +000033#include <cygwin/version.h>
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000034#include <sys/cygwin.h>
35#define LLVM_ON_WIN32 1
36#endif
37
Ted Kremenekab188932010-01-05 19:32:54 +000038#ifdef LLVM_ON_WIN32
39#include <windows.h>
40#else
41#include <dlfcn.h>
42#endif
43
44using namespace clang;
Ted Kremenekab188932010-01-05 19:32:54 +000045
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000046std::string CIndexer::getClangResourcesPath() {
Ted Kremenekab188932010-01-05 19:32:54 +000047 // Did we already compute the path?
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000048 if (!ResourcesPath.empty())
49 return ResourcesPath.str();
50
51 // Find the location where this library lives (libclang.dylib).
Ted Kremenekab188932010-01-05 19:32:54 +000052#ifdef LLVM_ON_WIN32
53 MEMORY_BASIC_INFORMATION mbi;
54 char path[MAX_PATH];
55 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
56 sizeof(mbi));
57 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000058
59#ifdef __CYGWIN__
60 char w32path[MAX_PATH];
61 strcpy(w32path, path);
NAKAMURA Takumi893793b2011-03-08 22:17:33 +000062#if CYGWIN_VERSION_API_MAJOR > 0 || CYGWIN_VERSION_API_MINOR >= 181
63 cygwin_conv_path(CCP_WIN_A_TO_POSIX, w32path, path, MAX_PATH);
64#else
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000065 cygwin_conv_to_full_posix_path(w32path, path);
66#endif
NAKAMURA Takumi893793b2011-03-08 22:17:33 +000067#endif
NAKAMURA Takumi3b35a4d2010-11-09 03:25:16 +000068
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000069 llvm::sys::Path LibClangPath(path);
70 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000071#else
72 // This silly cast below avoids a C++ warning.
73 Dl_info info;
74 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
David Blaikieb219cfc2011-09-23 05:06:16 +000075 llvm_unreachable("Call to dladdr() failed");
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000076
77 llvm::sys::Path LibClangPath(info.dli_fname);
78
Ted Kremenekab188932010-01-05 19:32:54 +000079 // We now have the CIndex directory, locate clang relative to it.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000080 LibClangPath.eraseComponent();
Ted Kremenekab188932010-01-05 19:32:54 +000081#endif
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000082
83 LibClangPath.appendComponent("clang");
84 LibClangPath.appendComponent(CLANG_VERSION_STRING);
Ted Kremenekab188932010-01-05 19:32:54 +000085
86 // Cache our result.
Douglas Gregord1e6fdb2010-10-11 23:17:59 +000087 ResourcesPath = LibClangPath;
88 return LibClangPath.str();
Ted Kremenekab188932010-01-05 19:32:54 +000089}
Douglas Gregor4db64a42010-01-23 00:14:00 +000090
Benjamin Kramerc2a98162010-03-13 21:22:49 +000091static llvm::sys::Path GetTemporaryPath() {
Daniel Dunbar74198af2010-02-02 05:19:57 +000092 // FIXME: This is lame; sys::Path should provide this function (in particular,
93 // it should know how to find the temporary files dir).
94 std::string Error;
95 const char *TmpDir = ::getenv("TMPDIR");
96 if (!TmpDir)
97 TmpDir = ::getenv("TEMP");
98 if (!TmpDir)
99 TmpDir = ::getenv("TMP");
100 if (!TmpDir)
101 TmpDir = "/tmp";
102 llvm::sys::Path P(TmpDir);
Benjamin Kramerc2a98162010-03-13 21:22:49 +0000103 P.appendComponent("remap");
Daniel Dunbar74198af2010-02-02 05:19:57 +0000104 if (P.makeUnique(false, &Error))
105 return llvm::sys::Path("");
106
107 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
108 P.eraseFromDisk(false, 0);
109
110 return P;
111}
112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113bool clang::RemapFiles(unsigned num_unsaved_files,
114 struct CXUnsavedFile *unsaved_files,
115 std::vector<std::string> &RemapArgs,
116 std::vector<llvm::sys::Path> &TemporaryFiles) {
117 for (unsigned i = 0; i != num_unsaved_files; ++i) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000118 // Write the contents of this unsaved file into the temporary file.
Benjamin Kramerc2a98162010-03-13 21:22:49 +0000119 llvm::sys::Path SavedFile(GetTemporaryPath());
Daniel Dunbar74198af2010-02-02 05:19:57 +0000120 if (SavedFile.empty())
121 return true;
122
Douglas Gregor4db64a42010-01-23 00:14:00 +0000123 std::string ErrorInfo;
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000124 llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo,
125 llvm::raw_fd_ostream::F_Binary);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000126 if (!ErrorInfo.empty())
127 return true;
128
129 OS.write(unsaved_files[i].Contents, unsaved_files[i].Length);
130 OS.close();
131 if (OS.has_error()) {
132 SavedFile.eraseFromDisk();
Dan Gohmanebaf2572010-05-27 20:16:37 +0000133 OS.clear_error();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000134 return true;
135 }
136
137 // Remap the file.
138 std::string RemapArg = unsaved_files[i].Filename;
139 RemapArg += ';';
Daniel Dunbar74198af2010-02-02 05:19:57 +0000140 RemapArg += SavedFile.str();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000141 RemapArgs.push_back("-Xclang");
142 RemapArgs.push_back("-remap-file");
143 RemapArgs.push_back("-Xclang");
144 RemapArgs.push_back(RemapArg);
145 TemporaryFiles.push_back(SavedFile);
146 }
147
148 return false;
149}
150