Skip to content

Commit

Permalink
Merge from 'master' to 'sycl-web' (intel#56)
Browse files Browse the repository at this point in the history
Resolved conflicts:
  CONFLICT (content): Merge conflict in README.md
  CONFLICT (add/add): Merge conflict in CONTRIBUTING.md

Signed-off-by: Vladimir Lazarev <vladimir.lazarev@intel.com>
  • Loading branch information
vladimirlaz committed Dec 3, 2019
2 parents 3f6b3f1 + effcdc3 commit 7292500
Show file tree
Hide file tree
Showing 184 changed files with 8,176 additions and 3,459 deletions.
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
# Contributing

## License
Expand Down Expand Up @@ -92,3 +93,15 @@ Merge of pull request is done only by project maintainers. There are three optio
Squashing is done to shorten history and make sure that the project is buildable on any commit.
- [Create a merge commit] Used for pull down PRs to avoid duplication of
LLVM commits.
=======
# Contributing to LLVM

Thank you for your interest in contributing to LLVM! There are many ways to
contribute, and we appreciate all contributions.

To get started with contributing, please take a look at the
[Contributing to LLVM](https://llvm.org/docs/Contributing.html) guide. It
describes how to get involved, raise issues and submit patches. Please note
that at the moment the LLVM project does not use either Github pull requests
or Github issues.
>>>>>>> effcdc3a82f2a32829170e7f7a2ff3d7853b612d
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

## Introduction

<<<<<<< HEAD
Intel staging area for llvm.org contribution.
Home for Intel LLVM-based projects:
- SYCL* Compiler and Runtimes - compiler and runtime libraries for SYCL ([https://www.khronos.org/sycl/](https://www.khronos.org/sycl/)). See **sycl** branch.
=======
The README briefly describes how to get started with building LLVM.
For more information on how to contribute to the LLVM project, please
take a look at the
[Contributing to LLVM](https://llvm.org/docs/Contributing.html) guide.

## Getting Started with the LLVM System
>>>>>>> effcdc3a82f2a32829170e7f7a2ff3d7853b612d
## License
See [LICENSE.txt](sycl/LICENSE.TXT) for details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static bool isSurroundedRight(const Token &T) {
/// Is given TokenKind a keyword?
static bool isKeyword(const Token &T) {
// FIXME: better matching of keywords to avoid false positives.
return T.isOneOf(tok::kw_case, tok::kw_const, tok::kw_struct);
return T.isOneOf(tok::kw_if, tok::kw_case, tok::kw_const, tok::kw_struct);
}

/// Warning is written when one of these operators are not within parentheses.
Expand Down
150 changes: 137 additions & 13 deletions clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include <string>
#include <tuple>
#include <vector>
Expand All @@ -27,6 +29,113 @@ namespace clang {
namespace clangd {
namespace {

// Query apple's `xcrun` launcher, which is the source of truth for "how should"
// clang be invoked on this system.
llvm::Optional<std::string> queryXcrun(llvm::ArrayRef<llvm::StringRef> Argv) {
auto Xcrun = llvm::sys::findProgramByName("xcrun");
if (!Xcrun) {
log("Couldn't find xcrun. Hopefully you have a non-apple toolchain...");
return llvm::None;
}
llvm::SmallString<64> OutFile;
llvm::sys::fs::createTemporaryFile("clangd-xcrun", "", OutFile);
llvm::FileRemover OutRemover(OutFile);
llvm::Optional<llvm::StringRef> Redirects[3] = {
/*stdin=*/{""}, /*stdout=*/{OutFile}, /*stderr=*/{""}};
vlog("Invoking {0} to find clang installation", *Xcrun);
int Ret = llvm::sys::ExecuteAndWait(*Xcrun, Argv,
/*Env=*/llvm::None, Redirects,
/*SecondsToWait=*/10);
if (Ret != 0) {
log("xcrun exists but failed with code {0}. "
"If you have a non-apple toolchain, this is OK. "
"Otherwise, try xcode-select --install.",
Ret);
return llvm::None;
}

auto Buf = llvm::MemoryBuffer::getFile(OutFile);
if (!Buf) {
log("Can't read xcrun output: {0}", Buf.getError().message());
return llvm::None;
}
StringRef Path = Buf->get()->getBuffer().trim();
if (Path.empty()) {
log("xcrun produced no output");
return llvm::None;
}
return Path.str();
}

// On Mac, `which clang` is /usr/bin/clang. It runs `xcrun clang`, which knows
// where the real clang is kept. We need to do the same thing,
// because cc1 (not the driver!) will find libc++ relative to argv[0].
llvm::Optional<std::string> queryMacClangPath() {
#ifndef __APPLE__
return llvm::None;
#endif

return queryXcrun({"xcrun", "--find", "clang"});
}

// Resolve symlinks if possible.
std::string resolve(std::string Path) {
llvm::SmallString<128> Resolved;
if (llvm::sys::fs::real_path(Path, Resolved))
return Path; // On error;
return Resolved.str();
}

// Get a plausible full `clang` path.
// This is used in the fallback compile command, or when the CDB returns a
// generic driver with no path.
llvm::StringRef getFallbackClangPath() {
static const std::string &MemoizedFallbackPath = [&]() -> std::string {
// The driver and/or cc1 sometimes depend on the binary name to compute
// useful things like the standard library location.
// We need to emulate what clang on this system is likely to see.
// cc1 in particular looks at the "real path" of the running process, and
// so if /usr/bin/clang is a symlink, it sees the resolved path.
// clangd doesn't have that luxury, so we resolve symlinks ourselves.

// /usr/bin/clang on a mac is a program that redirects to the right clang.
// We resolve it as if it were a symlink.
if (auto MacClang = queryMacClangPath())
return resolve(std::move(*MacClang));
// On other platforms, just look for compilers on the PATH.
for (const char* Name : {"clang", "gcc", "cc"})
if (auto PathCC = llvm::sys::findProgramByName(Name))
return resolve(std::move(*PathCC));
// Fallback: a nonexistent 'clang' binary next to clangd.
static int Dummy;
std::string ClangdExecutable =
llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
SmallString<128> ClangPath;
ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
llvm::sys::path::append(ClangPath, "clang");
return ClangPath.str();
}();
return MemoizedFallbackPath;
}

// On mac, /usr/bin/clang sets SDKROOT and then invokes the real clang.
// The effect of this is to set -isysroot correctly. We do the same.
const std::string *getMacSysroot() {
#ifndef __APPLE__
return nullptr;
#endif

// SDKROOT overridden in environment, respect it. Driver will set isysroot.
if (::getenv("SDKROOT"))
return nullptr;
static const llvm::Optional<std::string> &Sysroot =
queryXcrun({"xcrun", "--show-sdk-path"});
return Sysroot ? Sysroot.getPointer() : nullptr;
}

// Transform a command into the form we want to send to the driver.
// The command was originally either from the CDB or is the fallback command,
// and may have been modified by OverlayCDB.
void adjustArguments(tooling::CompileCommand &Cmd,
llvm::StringRef ResourceDir) {
tooling::ArgumentsAdjuster ArgsAdjuster = tooling::combineAdjusters(
Expand All @@ -40,10 +149,35 @@ void adjustArguments(tooling::CompileCommand &Cmd,
tooling::getClangSyntaxOnlyAdjuster()));

Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
// Check whether the flag exists, either as -flag or -flag=*
auto Has = [&](llvm::StringRef Flag) {
for (llvm::StringRef Arg : Cmd.CommandLine) {
if (Arg.consume_front(Flag) && (Arg.empty() || Arg[0] == '='))
return true;
}
return false;
};
// Inject the resource dir.
// FIXME: Don't overwrite it if it's already there.
if (!ResourceDir.empty())
if (!ResourceDir.empty() && !Has("-resource-dir"))
Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str());
if (!Has("-isysroot"))
if (const std::string *MacSysroot = getMacSysroot()) {
Cmd.CommandLine.push_back("-isysroot");
Cmd.CommandLine.push_back(*MacSysroot);
}

// If the driver is a generic name like "g++" with no path, add a clang path.
// This makes it easier for us to find the standard libraries on mac.
if (!Cmd.CommandLine.empty()) {
std::string &Driver = Cmd.CommandLine.front();
if (Driver == "clang" || Driver == "clang++" || Driver == "gcc" ||
Driver == "g++" || Driver == "cc" || Driver == "c++") {
llvm::SmallString<128> QualifiedDriver =
llvm::sys::path::parent_path(getFallbackClangPath());
llvm::sys::path::append(QualifiedDriver, Driver);
Driver = QualifiedDriver.str();
}
}
}

std::string getStandardResourceDir() {
Expand All @@ -63,19 +197,9 @@ void actOnAllParentDirectories(PathRef FileName,

} // namespace

static std::string getFallbackClangPath() {
static int Dummy;
std::string ClangdExecutable =
llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
SmallString<128> ClangPath;
ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
llvm::sys::path::append(ClangPath, "clang");
return ClangPath.str();
}

tooling::CompileCommand
GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
std::vector<std::string> Argv = {getFallbackClangPath()};
std::vector<std::string> Argv = {"clang"};
// Clang treats .h files as C by default and files without extension as linker
// input, resulting in unhelpful diagnostics.
// Parsing as Objective C++ is friendly to more cases.
Expand Down
Loading

0 comments on commit 7292500

Please sign in to comment.