2017-05-16 17:38:59 +08:00
|
|
|
//===--- GlobalCompilationDatabase.h ----------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_GLOBALCOMPILATIONDATABASE_H
|
|
|
|
|
|
|
|
#include "Path.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2017-05-16 18:06:20 +08:00
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
namespace tooling {
|
|
|
|
class CompilationDatabase;
|
|
|
|
struct CompileCommand;
|
|
|
|
} // namespace tooling
|
|
|
|
|
|
|
|
namespace clangd {
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
class Logger;
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Provides compilation arguments used for parsing C and C++ files.
|
2017-05-16 17:38:59 +08:00
|
|
|
class GlobalCompilationDatabase {
|
|
|
|
public:
|
|
|
|
virtual ~GlobalCompilationDatabase() = default;
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
/// If there are any known-good commands for building this file, returns one.
|
|
|
|
virtual llvm::Optional<tooling::CompileCommand>
|
|
|
|
getCompileCommand(PathRef File) const = 0;
|
|
|
|
|
|
|
|
/// Makes a guess at how to build a file.
|
|
|
|
/// The default implementation just runs clang on the file.
|
|
|
|
/// Clangd should treat the results as unreliable.
|
|
|
|
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
|
|
|
/// FIXME(ibiryukov): add facilities to track changes to compilation flags of
|
|
|
|
/// existing targets.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Gets compile args from tooling::CompilationDatabases built for parent
|
|
|
|
/// directories.
|
|
|
|
class DirectoryBasedGlobalCompilationDatabase
|
|
|
|
: public GlobalCompilationDatabase {
|
|
|
|
public:
|
2017-10-02 23:13:20 +08:00
|
|
|
DirectoryBasedGlobalCompilationDatabase(
|
2017-12-13 20:51:22 +08:00
|
|
|
llvm::Optional<Path> CompileCommandsDir);
|
2018-04-20 19:35:17 +08:00
|
|
|
~DirectoryBasedGlobalCompilationDatabase() override;
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
/// Scans File's parents looking for compilation databases.
|
|
|
|
/// Any extra flags will be added.
|
|
|
|
llvm::Optional<tooling::CompileCommand>
|
|
|
|
getCompileCommand(PathRef File) const override;
|
|
|
|
|
|
|
|
/// Uses the default fallback command, adding any extra flags.
|
|
|
|
tooling::CompileCommand getFallbackCommand(PathRef File) const override;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
[clangd] DidChangeConfiguration Notification
Summary:
Implementation of DidChangeConfiguration notification handling in
clangd. This currently only supports changing one setting: the path of
the compilation database to be used for the current project. In other
words, it is no longer necessary to restart clangd with a different
command line argument in order to change the compilation database.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: jkorous-apple, ioeric, simark, klimek, ilya-biryukov, arphaman, rwols, cfe-commits
Differential Revision: https://reviews.llvm.org/D39571
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325784
2018-02-22 22:00:39 +08:00
|
|
|
/// Set the compile commands directory to \p P.
|
|
|
|
void setCompileCommandsDir(Path P);
|
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
/// Sets the extra flags that should be added to a file.
|
2017-07-06 16:44:54 +08:00
|
|
|
void setExtraFlagsForFile(PathRef File, std::vector<std::string> ExtraFlags);
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
private:
|
2017-12-22 17:47:34 +08:00
|
|
|
tooling::CompilationDatabase *getCDBForFile(PathRef File) const;
|
|
|
|
tooling::CompilationDatabase *getCDBInDirLocked(PathRef File) const;
|
2017-12-04 18:08:45 +08:00
|
|
|
void addExtraFlags(PathRef File, tooling::CompileCommand &C) const;
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2017-12-04 18:08:45 +08:00
|
|
|
mutable std::mutex Mutex;
|
2017-05-16 17:38:59 +08:00
|
|
|
/// Caches compilation databases loaded from directories(keys are
|
|
|
|
/// directories).
|
2017-12-04 18:08:45 +08:00
|
|
|
mutable llvm::StringMap<std::unique_ptr<clang::tooling::CompilationDatabase>>
|
2017-05-16 17:38:59 +08:00
|
|
|
CompilationDatabases;
|
2017-07-06 16:44:54 +08:00
|
|
|
|
|
|
|
/// Stores extra flags per file.
|
|
|
|
llvm::StringMap<std::vector<std::string>> ExtraFlagsForFile;
|
2017-10-02 23:13:20 +08:00
|
|
|
/// Used for command argument pointing to folder where compile_commands.json
|
|
|
|
/// is located.
|
|
|
|
llvm::Optional<Path> CompileCommandsDir;
|
2017-05-16 17:38:59 +08:00
|
|
|
};
|
2018-06-13 17:20:41 +08:00
|
|
|
|
|
|
|
/// A wrapper around GlobalCompilationDatabase that caches the compile commands.
|
|
|
|
/// Note that only results of getCompileCommand are cached.
|
|
|
|
class CachingCompilationDb : public GlobalCompilationDatabase {
|
|
|
|
public:
|
|
|
|
explicit CachingCompilationDb(const GlobalCompilationDatabase &InnerCDB);
|
|
|
|
|
|
|
|
/// Gets compile command for \p File from cache or CDB if it's not in the
|
|
|
|
/// cache.
|
|
|
|
llvm::Optional<tooling::CompileCommand>
|
|
|
|
getCompileCommand(PathRef File) const override;
|
|
|
|
|
|
|
|
/// Forwards to the inner CDB. Results of this function are not cached.
|
|
|
|
tooling::CompileCommand getFallbackCommand(PathRef File) const override;
|
|
|
|
|
|
|
|
/// Removes an entry for \p File if it's present in the cache.
|
|
|
|
void invalidate(PathRef File);
|
|
|
|
|
|
|
|
/// Removes all cached compile commands.
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const GlobalCompilationDatabase &InnerCDB;
|
|
|
|
mutable std::mutex Mut;
|
|
|
|
mutable llvm::StringMap<llvm::Optional<tooling::CompileCommand>>
|
|
|
|
Cached; /* GUARDED_BY(Mut) */
|
|
|
|
};
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|