Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess compile commands in irony-server. #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "server/src/rapidjson"]
path = server/src/rapidjson
url = https://github.com/miloyip/rapidjson
70 changes: 0 additions & 70 deletions irony-cdb-libclang.el

This file was deleted.

87 changes: 87 additions & 0 deletions irony-cdb-server.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
;;; irony-cdb-server.el --- Compilation Database querying irony-server

;; Copyright (C) 2015 Karl Hylén

;; Author: Karl Hylén <[email protected]>
;; Keywords: c, convenience, tools

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'irony-cdb)
(require 'irony-cdb-json)

(require 'cl-lib)

;;;###autoload
(defun irony-cdb-server (command &rest args)
(cl-case command
(get-compile-options (irony-cdb-server--get-compile-options))))

(defun irony-cdb-server--has-database ()
"Return t if database is enabled."
(irony--send-request-sync "has-compile-db"))

(defun irony-cdb-server--get-compile-options ()
(irony--awhen (irony-cdb-json--locate-db)
(or (irony-cdb-server--server-exact-flags buffer-file-name it)
(irony-cdb-server--server-guess-flags buffer-file-name it))))

(defun irony-cdb-server--server-exact-flags (src-file db-file)
"Get compilation options from irony-server.

The parameter SRC-FILE is the source file we seek the compile command of and
DB-FILE is the database file."
(irony-cdb-server--adjust-options-and-remove-compiler
src-file
(irony--send-request-sync "get-compile-options"
db-file
src-file)))

(defun irony-cdb-server--server-guess-flags (src-file db-file)
"Make irony-server guess compilation arguments of a file.

The parameter SRC-FILE is the source file we seek the compile command of and
DB-FILE is the database file."
(let* ((guess
(irony--send-request-sync "guess-compile-options"
db-file
src-file))
(guessed-file (car guess))
(guessed-cmd (cdr guess)))
(irony-cdb-server--adjust-options-and-remove-compiler
guessed-file
(list guessed-cmd))))

(defun irony-cdb-server--adjust-options-and-remove-compiler (file cmds)
"Remove compiler, target file FILE and output file from CMDS.

The parameter CMDS is a list of conses. In each cons, the car holds the options
and the cdr holds the working directory where the compile command was issued."
(mapcar (lambda (cmd)
(let ((opt (irony-cdb--remove-compiler-from-flags (car cmd)))
(wdir (cdr cmd)))
(cons
(irony-cdb-json--adjust-compile-options opt file wdir)
wdir)))
cmds))

(provide 'irony-cdb-server)

;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:

;;; irony-cdb-server ends here
4 changes: 2 additions & 2 deletions irony-cdb.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

(autoload 'irony-cdb-clang-complete "irony-cdb-clang-complete")
(autoload 'irony-cdb-json "irony-cdb-json")
(autoload 'irony-cdb-libclang "irony-cdb-libclang")
(autoload 'irony-cdb-server "irony-cdb-server")


;;
Expand All @@ -48,7 +48,7 @@
:group 'irony)

(defcustom irony-cdb-compilation-databases '(irony-cdb-clang-complete
irony-cdb-libclang
irony-cdb-server
irony-cdb-json)
"List of active compilation databases.

Expand Down
20 changes: 17 additions & 3 deletions irony.el
Original file line number Diff line number Diff line change
Expand Up @@ -544,22 +544,26 @@ The installation requires CMake and the libclang developpement package."

When using a leading space, the buffer is hidden from the buffer
list (and undo information is not kept).")
(defvar irony--server-log nil
"The log file of irony-server.")

(defun irony--start-server-process ()
(when (setq irony--server-executable (or irony--server-executable
(irony--locate-server-executable)))
(let ((process-connection-type nil)
(process-adaptive-read-buffering nil)
process)
(setq irony--server-log (expand-file-name
(format-time-string
"irony.%Y-%m-%d_%Hh-%Mm-%Ss.log")
temporary-file-directory))
(setq process
(start-process-shell-command
"Irony" ;process name
irony--server-buffer ;buffer
(format "%s -i 2> %s" ;command
(shell-quote-argument irony--server-executable)
(expand-file-name
(format-time-string "irony.%Y-%m-%d_%Hh-%Mm-%Ss.log")
temporary-file-directory))))
irony--server-log)))
(buffer-disable-undo irony--server-buffer)
(set-process-query-on-exit-flag process nil)
(set-process-sentinel process 'irony--server-process-sentinel)
Expand All @@ -574,6 +578,16 @@ list (and undo information is not kept).")
(kill-process irony--server-process)
(setq irony--server-process nil)))

;;;###autoload
(defun irony-open-log-file ()
"Open irony server log file"
(interactive)
(if (and irony--server-log (file-exists-p irony--server-log))
(progn
(find-file irony--server-log)
(auto-revert-tail-mode))
(message "Log file doesn't exist yet!")))

(defun irony--get-server-process-create ()
(if (and irony--server-process
(process-live-p irony--server-process))
Expand Down
2 changes: 1 addition & 1 deletion server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
endif()

if(IRONY_COMPILER_IS_GCC_COMPATIBLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
elseif(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c- /D_HAS_EXCEPTIONS=0")
# irony-server uses some code that breaks when iterator debugging is enabled
Expand Down
19 changes: 18 additions & 1 deletion server/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ include(CheckLibClangBuiltinHeadersDir)

find_package(LibClang REQUIRED)

find_package(Boost 1.60.0 COMPONENTS system filesystem)

include_directories(${LIBCLANG_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(rapidjson/include)

check_libclang_builtin_headers_dir()

Expand All @@ -15,7 +18,7 @@ endif()
# not to be taken as a module-definition file to link on Windows
set_source_files_properties(Commands.def PROPERTIES HEADER_FILE_ONLY TRUE)

add_executable(irony-server
set(IRONY_SOURCES
support/arraysize.h
support/CommandLineParser.cpp
support/CommandLineParser.h
Expand All @@ -35,6 +38,14 @@ add_executable(irony-server

main.cpp)

if (Boost_FOUND)
message("-- Found boost_filesystem, enabling server compilation database!")
add_definitions(-DHAS_BOOST_FILESYSTEM)
set(IRONY_SOURCES ${IRONY_SOURCES} CompilationDatabase.cpp)
endif()

add_executable(irony-server ${IRONY_SOURCES})

# retrieve the package version from irony.el
function(irony_find_package_version OUTPUT_VAR)
# this is a hack that force CMake to reconfigure, it is necessary to see if
Expand Down Expand Up @@ -67,4 +78,10 @@ set_source_files_properties(main.cpp

target_link_libraries(irony-server ${LIBCLANG_LIBRARIES})

if (Boost_FOUND)
target_link_libraries(irony-server ${Boost_LIBRARIES})
endif()

# add_subdirectory(json-test)

install(TARGETS irony-server DESTINATION bin)
6 changes: 4 additions & 2 deletions server/src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::ostream &operator<<(std::ostream &os, const Command::Action &action) {
std::ostream &operator<<(std::ostream &os, const Command &command) {
os << "Command{action=" << command.action << ", "
<< "file='" << command.file << "', "
<< "dir='" << command.dir << "', "
<< "db='" << command.db << "', "
<< "line=" << command.line << ", "
<< "column=" << command.column << ", "
<< "flags=[";
Expand Down Expand Up @@ -169,11 +169,13 @@ Command *CommandParser::parse(const std::vector<std::string> &argv) {
case Command::Diagnostics:
case Command::Help:
case Command::Exit:
case Command::HasCompilationDatabase:
// no-arguments commands
break;

case Command::GetCompileOptions:
positionalArgs.push_back(StringConverter(&command_.dir));
case Command::GuessCompileOptions:
positionalArgs.push_back(StringConverter(&command_.db));
positionalArgs.push_back(StringConverter(&command_.file));
break;

Expand Down
4 changes: 2 additions & 2 deletions server/src/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Command {
action = Unknown;
flags.clear();
file.clear();
dir.clear();
db.clear();
line = 0;
column = 0;
unsavedFiles.clear();
Expand All @@ -44,7 +44,7 @@ struct Command {

std::vector<std::string> flags;
std::string file;
std::string dir;
std::string db;
unsigned line;
unsigned column;
// pair of (filename, content)
Expand Down
3 changes: 3 additions & 0 deletions server/src/Commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ X(Diagnostics, "diagnostics", "print the diagnostics of the last parse")
X(Exit, "exit", "exit interactive mode, print nothing")
X(GetCompileOptions, "get-compile-options", "BUILD_DIR FILE - "
"get compile options for FILE from JSON database in PROJECT_ROOT")
X(GuessCompileOptions, "guess-compile-options", "BUILD_DIR FILE - "
"guess compile options for FILE from JSON database in PROJECT_ROOT")
X(HasCompilationDatabase, "has-compile-db", "Print t if database is enabled.")
X(Help, "help", "show this message")
X(Parse, "parse", "FILE - parse the given file")
X(SetDebug, "set-debug", "[on|off] - enable or disable verbose logging")
Expand Down
Loading