blob: 4cbf0c19510a4fa0edfeb98e3002a44c45483a5c [file] [log] [blame]
Ted Kremenek0ec2cca2010-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 Kremenek0ec2cca2010-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 Noblesmith1ced7372011-12-22 22:49:47 +000023#include "llvm/Config/llvm-config.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000026#include "llvm/Support/Program.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000027#include "llvm/Support/raw_ostream.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000028#include <cstdio>
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000029#include <sstream>
Chandler Carruthcc0694c2012-12-04 09:25:21 +000030#include <vector>
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000031
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000032#ifdef __CYGWIN__
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000033#include <cygwin/version.h>
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000034#include <sys/cygwin.h>
35#define LLVM_ON_WIN32 1
36#endif
37
Ted Kremenek0ec2cca2010-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 Kremenek0ec2cca2010-01-05 19:32:54 +000045
Benjamin Kramere3868e42013-06-13 13:56:37 +000046const std::string &CIndexer::getClangResourcesPath() {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000047 // Did we already compute the path?
Douglas Gregorbd829982010-10-11 23:17:59 +000048 if (!ResourcesPath.empty())
Benjamin Kramere3868e42013-06-13 13:56:37 +000049 return ResourcesPath;
50
51 SmallString<128> LibClangPath;
52
Douglas Gregorbd829982010-10-11 23:17:59 +000053 // Find the location where this library lives (libclang.dylib).
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000054#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 Takumiaa63fdf2010-11-09 03:25:16 +000060
61#ifdef __CYGWIN__
62 char w32path[MAX_PATH];
63 strcpy(w32path, path);
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000064#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 Takumiaa63fdf2010-11-09 03:25:16 +000067 cygwin_conv_to_full_posix_path(w32path, path);
68#endif
NAKAMURA Takumid24228a2011-03-08 22:17:33 +000069#endif
NAKAMURA Takumiaa63fdf2010-11-09 03:25:16 +000070
Benjamin Kramere3868e42013-06-13 13:56:37 +000071 LibClangPath += llvm::sys::path::parent_path(path);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000072#else
73 // This silly cast below avoids a C++ warning.
74 Dl_info info;
75 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
David Blaikie83d382b2011-09-23 05:06:16 +000076 llvm_unreachable("Call to dladdr() failed");
Benjamin Kramere3868e42013-06-13 13:56:37 +000077
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000078 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramere3868e42013-06-13 13:56:37 +000079 LibClangPath += llvm::sys::path::parent_path(info.dli_fname);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000080#endif
Benjamin Kramere3868e42013-06-13 13:56:37 +000081
82 llvm::sys::path::append(LibClangPath, "clang", CLANG_VERSION_STRING);
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000083
84 // Cache our result.
Benjamin Kramere3868e42013-06-13 13:56:37 +000085 ResourcesPath = LibClangPath.str();
86 return ResourcesPath;
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000087}
Douglas Gregoraa98ed92010-01-23 00:14:00 +000088
Benjamin Kramerf156b9d2010-03-13 21:22:49 +000089static llvm::sys::Path GetTemporaryPath() {
Daniel Dunbar64ee5352010-02-02 05:19:57 +000090 // FIXME: This is lame; sys::Path should provide this function (in particular,
91 // it should know how to find the temporary files dir).
92 std::string Error;
93 const char *TmpDir = ::getenv("TMPDIR");
94 if (!TmpDir)
95 TmpDir = ::getenv("TEMP");
96 if (!TmpDir)
97 TmpDir = ::getenv("TMP");
98 if (!TmpDir)
99 TmpDir = "/tmp";
100 llvm::sys::Path P(TmpDir);
Benjamin Kramerf156b9d2010-03-13 21:22:49 +0000101 P.appendComponent("remap");
Daniel Dunbar64ee5352010-02-02 05:19:57 +0000102 if (P.makeUnique(false, &Error))
103 return llvm::sys::Path("");
104
105 // FIXME: Grumble, makeUnique sometimes leaves the file around!? PR3837.
106 P.eraseFromDisk(false, 0);
107
108 return P;
109}
110
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000111bool clang::RemapFiles(unsigned num_unsaved_files,
112 struct CXUnsavedFile *unsaved_files,
113 std::vector<std::string> &RemapArgs,
114 std::vector<llvm::sys::Path> &TemporaryFiles) {
115 for (unsigned i = 0; i != num_unsaved_files; ++i) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000116 // Write the contents of this unsaved file into the temporary file.
Benjamin Kramerf156b9d2010-03-13 21:22:49 +0000117 llvm::sys::Path SavedFile(GetTemporaryPath());
Daniel Dunbar64ee5352010-02-02 05:19:57 +0000118 if (SavedFile.empty())
119 return true;
120
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000121 std::string ErrorInfo;
Francois Pichet85e6da12010-10-12 01:01:43 +0000122 llvm::raw_fd_ostream OS(SavedFile.c_str(), ErrorInfo,
123 llvm::raw_fd_ostream::F_Binary);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000124 if (!ErrorInfo.empty())
125 return true;
126
127 OS.write(unsaved_files[i].Contents, unsaved_files[i].Length);
128 OS.close();
129 if (OS.has_error()) {
130 SavedFile.eraseFromDisk();
Dan Gohmanef832a62010-05-27 20:16:37 +0000131 OS.clear_error();
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000132 return true;
133 }
134
135 // Remap the file.
136 std::string RemapArg = unsaved_files[i].Filename;
137 RemapArg += ';';
Daniel Dunbar64ee5352010-02-02 05:19:57 +0000138 RemapArg += SavedFile.str();
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000139 RemapArgs.push_back("-Xclang");
140 RemapArgs.push_back("-remap-file");
141 RemapArgs.push_back("-Xclang");
142 RemapArgs.push_back(RemapArg);
143 TemporaryFiles.push_back(SavedFile);
144 }
145
146 return false;
147}
148