diff options
author | mattkae <mattkae@protonmail.com> | 2022-05-11 09:23:58 -0400 |
---|---|---|
committer | mattkae <mattkae@protonmail.com> | 2022-05-11 09:23:58 -0400 |
commit | 3f4a0d5370ae6c34afe180df96add3b8522f4af1 (patch) | |
tree | ae901409e02bde8ee278475f8cf6818f8f680a60 /elpa/irony-20220110.849/server/src/support |
initial commit
Diffstat (limited to 'elpa/irony-20220110.849/server/src/support')
7 files changed, 369 insertions, 0 deletions
diff --git a/elpa/irony-20220110.849/server/src/support/CIndex.h b/elpa/irony-20220110.849/server/src/support/CIndex.h new file mode 100644 index 0000000..89d3f62 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/CIndex.h @@ -0,0 +1,33 @@ +/** + * \file + * \brief Wrapper around Clang Indexing Public C Interface header. + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#ifndef IRONY_MODE_SERVER_SUPPORT_CINDEXVERSION_H_ +#define IRONY_MODE_SERVER_SUPPORT_CINDEXVERSION_H_ + +#include <clang-c/Index.h> + +/// Use <tt>\#if CINDEX_VERSION_VERSION > 10047</tt> to test for +/// CINDEX_VERSION_MAJOR = 1 and CINDEX_VERSION_MINOR = 47. +#ifndef CINDEX_VERSION +#define CINDEX_VERSION 0 // pre-clang 3.2 support +#endif + +#if CINDEX_VERSION >= 6 +#define HAS_BRIEF_COMMENTS_IN_COMPLETION 1 +#else +#define HAS_BRIEF_COMMENTS_IN_COMPLETION 0 +#endif + +#if CINDEX_VERSION >= 6 +#define HAS_COMPILATION_DATABASE 1 +#include <clang-c/CXCompilationDatabase.h> +#else +#define HAS_COMPILATION_DATABASE 0 +#endif + +#endif /* !IRONY_MODE_SERVER_SUPPORT_CINDEXVERSION_H_ */ diff --git a/elpa/irony-20220110.849/server/src/support/CommandLineParser.cpp b/elpa/irony-20220110.849/server/src/support/CommandLineParser.cpp new file mode 100644 index 0000000..a838092 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/CommandLineParser.cpp @@ -0,0 +1,119 @@ +/** + * \file + * \author Guillaume Papin <guillaume.papin@epitech.eu> + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#include "CommandLineParser.h" + +namespace { + +/// \brief A parser for escaped strings of command line arguments. +/// +/// Assumes \-escaping for quoted arguments (see the documentation of +/// unescapeCommandLine(...)). +class CommandLineArgumentParser { +public: + CommandLineArgumentParser(const std::string &commandLine) + : input_(commandLine), position_(input_.begin() - 1) { + } + + std::vector<std::string> parse() { + bool hasMoreInput = true; + while (hasMoreInput && nextNonWhitespace()) { + std::string argument; + hasMoreInput = parseStringInto(argument); + commandLine_.push_back(argument); + } + return commandLine_; + } + +private: + // All private methods return true if there is more input available. + + bool parseStringInto(std::string &string) { + do { + if (*position_ == '"') { + if (!parseDoubleQuotedStringInto(string)) + return false; + } else if (*position_ == '\'') { + if (!parseSingleQuotedStringInto(string)) + return false; + } else { + if (!parseFreeStringInto(string)) + return false; + } + } while (*position_ != ' '); + return true; + } + + bool parseDoubleQuotedStringInto(std::string &string) { + if (!next()) + return false; + while (*position_ != '"') { + if (!skipEscapeCharacter()) + return false; + string.push_back(*position_); + if (!next()) + return false; + } + return next(); + } + + bool parseSingleQuotedStringInto(std::string &string) { + if (!next()) + return false; + while (*position_ != '\'') { + string.push_back(*position_); + if (!next()) + return false; + } + return next(); + } + + bool parseFreeStringInto(std::string &string) { + do { + if (!skipEscapeCharacter()) + return false; + string.push_back(*position_); + if (!next()) + return false; + } while (*position_ != ' ' && *position_ != '"' && *position_ != '\''); + return true; + } + + bool skipEscapeCharacter() { + if (*position_ == '\\') { + return next(); + } + return true; + } + + bool nextNonWhitespace() { + do { + if (!next()) + return false; + } while (*position_ == ' '); + return true; + } + + bool next() { + ++position_; + return position_ != input_.end(); + } + +private: + const std::string input_; + std::string::const_iterator position_; + std::vector<std::string> commandLine_; +}; + +} // unnamed namespace + +std::vector<std::string> +unescapeCommandLine(const std::string &escapedCommandLine) { + CommandLineArgumentParser parser(escapedCommandLine); + return parser.parse(); +} diff --git a/elpa/irony-20220110.849/server/src/support/CommandLineParser.h b/elpa/irony-20220110.849/server/src/support/CommandLineParser.h new file mode 100644 index 0000000..b764797 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/CommandLineParser.h @@ -0,0 +1,21 @@ +/** + * \file + * \brief Facility to parse a command line into a string array. + * + * \note Please note that the code borrowed from the Clang, + * lib/Tooling/JSONCompilationDatabase.cpp. + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#ifndef IRONY_MODE_SERVER_SUPPORT_COMMAND_LINE_PARSER_H_ +#define IRONY_MODE_SERVER_SUPPORT_COMMAND_LINE_PARSER_H_ + +#include <string> +#include <vector> + +std::vector<std::string> +unescapeCommandLine(const std::string &escapedCommandLine); + +#endif // IRONY_MODE_SERVER_SUPPORT_COMMAND_LINE_PARSER_H_ diff --git a/elpa/irony-20220110.849/server/src/support/NonCopyable.h b/elpa/irony-20220110.849/server/src/support/NonCopyable.h new file mode 100644 index 0000000..d30a5b2 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/NonCopyable.h @@ -0,0 +1,34 @@ +/**-*-C++-*- + * \file + * \author Guillaume Papin <guillaume.papin@epitech.eu> + * + * \brief NonCopyable class like in Boost. + * + * \see http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-copyable_Mixin + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#ifndef IRONY_MODE_SERVER_SUPPORT_NONCOPYABLE_H_ +#define IRONY_MODE_SERVER_SUPPORT_NONCOPYABLE_H_ + +namespace util { + +class NonCopyable { +protected: + NonCopyable() { + } + + // Protected non-virtual destructor + ~NonCopyable() { + } + +private: + NonCopyable(const NonCopyable &); + NonCopyable &operator=(const NonCopyable &); +}; + +} // ! namespace util + +#endif /* !IRONY_MODE_SERVER_SUPPORT_NONCOPYABLE_H_ */ diff --git a/elpa/irony-20220110.849/server/src/support/TemporaryFile.cpp b/elpa/irony-20220110.849/server/src/support/TemporaryFile.cpp new file mode 100644 index 0000000..e7393e1 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/TemporaryFile.cpp @@ -0,0 +1,74 @@ +/** + * \file + * \author Guillaume Papin <guillaume.papin@epitech.eu> + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#include "TemporaryFile.h" + +#include <algorithm> +#include <cstdio> +#include <cstdlib> +#include <fstream> +#include <iostream> +#include <random> + +static std::string getTemporaryFileDirectory() { + const char *temporaryDirEnvVars[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; + + for (const char *envVar : temporaryDirEnvVars) { + if (const char *dir = std::getenv(envVar)) + return dir; + } + + return "/tmp"; +} + +TemporaryFile::TemporaryFile(const std::string &prefix, + const std::string &suffix) + : pathOrPattern_(prefix + "-%%%%%%" + suffix) { +} + +TemporaryFile::~TemporaryFile() { + if (openedFile_) { + openedFile_.reset(); + std::remove(pathOrPattern_.c_str()); + } +} + +const std::string &TemporaryFile::getPath() { + if (!openedFile_) { + openedFile_.reset(new std::fstream); + + std::random_device rd; + std::default_random_engine e(rd()); + std::uniform_int_distribution<int> dist(0, 15); + std::string pattern = pathOrPattern_; + std::string tmpDir = getTemporaryFileDirectory() + "/"; + int i = 0; + + do { + // exiting is better than infinite loop + if (++i > TemporaryFile::MAX_ATTEMPS) { + std::cerr << "error: couldn't create temporary file, please check your " + "temporary file directory (" << tmpDir << ")\n"; + exit(EXIT_FAILURE); + } + + // make the filename based on the pattern + std::transform(pattern.begin(), + pattern.end(), + pathOrPattern_.begin(), + [&e, &dist](char ch) { + return ch == '%' ? "0123456789abcdef"[dist(e)] : ch; + }); + // create the file + openedFile_->open(tmpDir + pathOrPattern_, std::ios_base::out); + } while (!openedFile_->is_open()); + pathOrPattern_ = tmpDir + pathOrPattern_; + } + + return pathOrPattern_; +} diff --git a/elpa/irony-20220110.849/server/src/support/TemporaryFile.h b/elpa/irony-20220110.849/server/src/support/TemporaryFile.h new file mode 100644 index 0000000..5bf77f4 --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/TemporaryFile.h @@ -0,0 +1,36 @@ +/**-*-C++-*- + * \file + * \author Guillaume Papin <guillaume.papin@epitech.eu> + * + * \brief Not the best piece of code out there. + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#ifndef IRONY_MODE_SERVER_SUPPORT_TEMPORARY_FILE_H_ +#define IRONY_MODE_SERVER_SUPPORT_TEMPORARY_FILE_H_ + +#include <iosfwd> +#include <memory> +#include <string> + +class TemporaryFile { + enum { + // if we can't create the temp file, exits. + MAX_ATTEMPS = 25 + }; + +public: + TemporaryFile(const std::string &prefix, const std::string &suffix = ""); + ~TemporaryFile(); + + /// Returns the path of this temporary filename + const std::string &getPath(); + +private: + std::string pathOrPattern_; + std::unique_ptr<std::fstream> openedFile_; +}; + +#endif // IRONY_MODE_SERVER_SUPPORT_TEMPORARY_FILE_H_ diff --git a/elpa/irony-20220110.849/server/src/support/iomanip_quoted.h b/elpa/irony-20220110.849/server/src/support/iomanip_quoted.h new file mode 100644 index 0000000..a8ca38b --- /dev/null +++ b/elpa/irony-20220110.849/server/src/support/iomanip_quoted.h @@ -0,0 +1,52 @@ +/**-*-C++-*- + * \file + * \brief Dumb implementation of something that might look like C++14 + * std::quoted. + * + * This file is distributed under the GNU General Public License. See + * COPYING for details. + */ + +#ifndef IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_ +#define IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_ + +#include <ostream> +#include <string> + +namespace support { +namespace detail { + +struct QuotedStringProxy { + QuotedStringProxy(const std::string &s) : s(s) { + } + + std::string s; +}; + +std::ostream &operator<<(std::ostream &os, const QuotedStringProxy &q) { + const std::string &s = q.s; + + os << '"'; + if (s.find_first_of("\"\\") == std::string::npos) { + os << s; + } else { + for (auto ch : s) { + if (ch == '\\' || ch == '"') + os << '\\'; + + os << ch; + } + } + os << '"'; + return os; +} + +} // namespace detail + +detail::QuotedStringProxy quoted(const std::string &s) { + return detail::QuotedStringProxy(s); +} + +} // namespace support + +#endif // IRONY_MODE_SERVER_SUPPORT_IOMANIP_QUOTED_H_ |