Skip to content

[emacs-support] More flymake support #5219

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

Merged
merged 1 commit into from
Oct 11, 2016
Merged
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
25 changes: 9 additions & 16 deletions .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@
;
;===----------------------------------------------------------------------===;
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.
;;; For more information see (info "(emacs) Directory Variables")

((nil
(tab-width . 2)
(fill-column . 80)
(eval .
;; Load the Swift project's settings. To suppress this action
;; you can put "(provide 'swift-project-settings)" in your
;; .emacs
(eval let* ((x (dir-locals-find-file default-directory))
(this-directory (if (listp x) (car x)
(file-name-directory x))))
(unless (featurep 'swift-project-settings)
;; Make sure the project's own utils directory is in the
;; load path, but don't override any one the user might have
;; set up.
(add-to-list
'load-path
(concat
(let ((dlff (dir-locals-find-file default-directory)))
(if (listp dlff) (car dlff) (file-name-directory dlff)))
"utils")
:append)
;; Load our project's settings -- indirectly brings in swift-mode
(add-to-list 'load-path (concat this-directory "utils") :append)
(let ((swift-project-directory this-directory))
(require 'swift-project-settings)))
(set (make-local-variable 'swift-project-directory) this-directory))
(c-file-style . "swift")
)
(c++-mode
Expand All @@ -44,6 +35,8 @@
(whitespace-style . (face lines indentation:space))
(eval . (whitespace-mode)))
(swift-mode
(swift-find-executable-fn . swift-project-executable-find)
(swift-syntax-check-fn . swift-project-swift-syntax-check)
(whitespace-style . (face lines indentation:space))
(eval . (whitespace-mode))
(swift-basic-offset . 2)
Expand Down
31 changes: 24 additions & 7 deletions utils/swift-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,35 @@ Use `M-x hs-show-all' to show them again."

(require 'flymake)

(defvar swift-find-executable-function 'executable-find
;; This name doesn't end in "function" to avoid being unconditionally marked as risky.
(defvar-local swift-find-executable-fn 'executable-find
"Function to find a command executable.
The function is called with one argument, the name of the executable to find.
Might be useful if you want to use a swiftc that you built instead
of the one in your PATH.")
(make-variable-buffer-local 'swift-find-executable-function)
(put 'swift-find-executable-fn 'safe-local-variable 'functionp)

(defvar swift-syntax-check-function 'swift-syntax-check-directory
(defvar-local swift-syntax-check-fn 'swift-syntax-check-directory
"Function to create the swift command-line that syntax-checks the current buffer.
The function is called with two arguments, the swiftc executable, and
the name of a temporary file that will contain the contents of the
current buffer.
Set to 'swift-syntax-check-single-file to ignore other files in the current directory.")
(make-variable-buffer-local 'swift-syntax-check-function)
(put 'swift-syntax-check-fn 'safe-local-variable 'functionp)

(defvar-local swift-syntax-check-args '("-parse")
"List of arguments to be passed to swiftc for syntax checking.
Elements of this list that are strings are inserted literally
into the command line. Elements that are S-expressions are
evaluated. The resulting list is cached in a file-local
variable, `swift-syntax-check-evaluated-args', so if you change
this variable you should set that one to nil.")
(put 'swift-syntax-check-args 'safe-local-variable 'listp)

(defvar-local swift-syntax-check-evaluated-args
"File-local cache of swift arguments used for syntax checking
variable, `swift-syntax-check-args', so if you change
that variable you should set this one to nil.")

(defun swift-syntax-check-single-file (swiftc temp-file)
"Return a flymake command-line list for syntax-checking the current buffer in isolation"
Expand All @@ -447,9 +462,11 @@ directory."
(make-temp-file
(concat (file-name-nondirectory x) "-" y)
(not :DIR_FLAG)
(concat "." (file-name-extension (buffer-file-name))))))))
(funcall swift-syntax-check-function
(funcall swift-find-executable-function "swiftc")
;; grab *all* the extensions; handles .swift.gyb files, for example
;; whereas using file-name-extension would only get ".gyb"
(replace-regexp-in-string "^\\(?:.*/\\)?[^.]*" "" (buffer-file-name)))))))
(funcall swift-syntax-check-fn
(funcall swift-find-executable-fn "swiftc")
temp-file)))

(add-to-list 'flymake-allowed-file-name-masks '(".+\\.swift$" flymake-swift-init))
Expand Down
170 changes: 169 additions & 1 deletion utils/swift-project-settings.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
(append '(("\\.swift$" . swift-mode) ("\\.gyb$" python-mode t)) auto-mode-alist))

;; Make sure we know where to find swift-mode
(autoload 'swift-mode "swift-mode"
(autoload 'swift-mode (concat (file-name-directory load-file-name) "swift-mode")
"Major mode for editing SWIFT source files.
\\{swift-mode-map}
Runs swift-mode-hook on startup."
Expand Down Expand Up @@ -255,5 +255,173 @@ takes precedence for files in the Swift project"
1 2 ,(not :column) ,(not :just-a-warning))
compilation-error-regexp-alist-alist)

(defvar swift-project-directory
(file-name-directory (directory-file-name (file-name-directory load-file-name)))
"Directory where the swift project containing this file is located.
Defaults to the parent directory of `swift-project-settings.el'.
The setting for file-local values of this variable comes from
.dir-locals.el in the project's root directory")
(put 'swift-project-directory 'safe-local-variable 'stringp)

(defun swift-project-default-build-directory (project-directory)
"Returns the default build directory given a project directory name, `DIR/../build'"
(concat (file-name-directory (directory-file-name project-directory)) "build/"))

;; This name doesn't end in "function" to avoid being unconditionally marked as risky.
(defcustom swift-project-build-directory-fn 'swift-project-default-build-directory
"A function that, given a swift project directory name,
computes the directory where your build leaves build products.
Flymake support may search here for a swift compiler to use, for example.
Defaults to `(concat swift-project-directory \"../build\")'."
:type 'function
)

(defun swift-project-executable-find (command)
"Find the newest executable with the given name in the utils/ directory or in any build directory, falling back to the exec-path as a last resort.
Given an absolute path, returns it verbatim. This is a pretty
good heuristic for locating things to use when working on swift
itself, and is used as the value of swift-find-executable-fn"
(if (file-name-absolute-p command) command
(let* ((utility (concat swift-project-directory "utils/" command))
(newest (and (file-executable-p utility) utility)))
(dolist (x (file-expand-wildcards
(concat (funcall swift-project-build-directory-fn swift-project-directory)
"*/swift-*/bin/" command)))
(when (and (file-executable-p x) (or (null newest) (file-newer-than-file-p x newest)))
(setq newest x)))
(or newest (executable-find command)))))

(defvar swift-project-sdk-path
(substring (shell-command-to-string "xcrun --show-sdk-path") 0 -1)
"The path to the swift SDK to use for syntax checking, etc.")

(defvar swift-project--gyb-temp-file-directory nil)
(defun swift-project-gyb-temp-file-directory ()
"A directory used for gyb-processed files"
(or swift-project--gyb-temp-file-directory
(setq swift-project--gyb-temp-file-directory
(make-temp-file "swift-project-gyb" :DIRECTORY))))

(defun swift-project-gyb-output-file-name (input-file-name)
"Given the name of a .gyb file, return the name of the temporary file we'll use for its expanded result."
(file-name-sans-extension
(expand-file-name
(subst-char-in-string
?/ ?! (replace-regexp-in-string
"!" "!!"
(if (file-name-absolute-p input-file-name) input-file-name
(expand-file-name input-file-name))))
(swift-project-gyb-temp-file-directory))))

(defun swift-project-gybbed-file (input-file-name)
"Given the name of a .gyb file, process it with gyb and return an output file name.
Given any other file name, just return that name."
(if (not (string-equal (file-name-extension input-file-name) "gyb"))
input-file-name
(let ((result (swift-project-gyb-output-file-name input-file-name)))
(prog1 result
(unless (file-newer-than-file-p result input-file-name)
(with-temp-buffer
(let* ((gyb (swift-project-executable-find "gyb"))
(status (call-process gyb nil t nil "-DCMAKE_SIZEOF_VOID_P=8" input-file-name)))
(unless (eq status 0)
(error "%s exited with status %s" gyb status)))
;; Use write-region instead of write-file to avoid spewing messages.
(write-region nil nil result nil 566)))))))

(defconst swift-project-stdlib-compile-order
"Algorithm ArrayBody ArrayBuffer ArrayBufferProtocol ArrayCast Arrays ArrayType Assert AssertCommon BidirectionalCollection Bool BridgeObjectiveC BridgeStorage Builtin BuiltinMath Character CocoaArray Collection CollectionAlgorithms Comparable CompilerProtocols ClosedRange ContiguousArrayBuffer CString CTypes DebuggerSupport DropWhile Dump EmptyCollection Equatable ErrorType Existential Filter FixedPoint FlatMap Flatten FloatingPoint FloatingPointParsing FloatingPointTypes Hashable HashedCollections AnyHashable HashedCollectionsAnyHashableExtensions Hashing HeapBuffer ImplicitlyUnwrappedOptional Index Indices InputStream IntegerArithmetic IntegerParsing Integers Join LazyCollection LazySequence LifetimeManager ManagedBuffer Map MemoryLayout Mirrors Misc MutableCollection NewtypeWrapper ObjCMirrors ObjectIdentifier Optional OptionSet OutputStream Pointer Policy PrefixWhile Print RandomAccessCollection Range RangeReplaceableCollection ReflectionLegacy Repeat REPL Reverse Runtime SipHash Sequence SequenceAlgorithms SequenceWrapper SetAlgebra ShadowProtocols Shims Slice Sort StaticString Stride StringCharacterView String StringBridge StringBuffer StringComparable StringCore StringHashable StringInterpolation StringLegacy StringRangeReplaceableCollection StringIndexConversions StringUnicodeScalarView StringUTF16 StringUTF8 SwiftNativeNSArray UnavailableStringAPIs Unicode UnicodeScalar UnicodeTrie Unmanaged UnsafeBitMap UnsafeBufferPointer UnsafeRawBufferPointer UnsafePointer UnsafeRawPointer WriteBackMutableSlice Availability CollectionOfOne ExistentialCollection Mirror CommandLine SliceBuffer Tuple UnfoldSequence VarArgs Zip"

"Unfortunately, the order in which we send files to the Swift compiler actually matters. We search this list to determine where each source file should go."
)

(defun swift-project-stdlib-compile-order (filename)
"Return an integer representing where in the required
compilation order the given file should appear."
(save-match-data
(if (string-match
(concat "\\<" (regexp-quote (replace-regexp-in-string "^\\(?:.*[/!]\\)?\\([^.]*\\).*" "\\1" filename)) "\\>")
swift-project-stdlib-compile-order)
(match-beginning 0) 0)))

(defconst swift-project-common-swiftc-args
(list "-parse" "-sdk" swift-project-sdk-path
"-F" (concat (file-name-as-directory swift-project-sdk-path) "../../../Developer/Library/Frameworks")
"-D" "INTERNAL_CHECKS_ENABLED"
"-no-link-objc-runtime")
"The common arguments we'll pass to swiftc for syntax-checking
everything in the Swift project" )

(defconst swift-project-single-frontend-swiftc-args
(append swift-project-common-swiftc-args
(list "-force-single-frontend-invocation" "-parse-as-library"))
"The arguments we'll pass to swiftc for syntax-checking
libraries that require a single frontend invocation" )

(defconst swift-project-stdlib-aux-swiftc-args
(append swift-project-single-frontend-swiftc-args
(list "-Xfrontend" "-sil-serialize-all" "-parse-stdlib"))
"swiftc arguments for library components that are compiled as
though they are part of the standard library even though
they're not strictly in that binary." )

(defconst swift-project-stdlib-swiftc-args
(append
swift-project-stdlib-aux-swiftc-args (list "-nostdimport" "-module-name" "Swift"))
"The arguments we'll pass to swiftc for syntax-checking the
standard library" )

(defun swift-project-files-to-compile-with (relative-file)
"Given RELATIVE-FILE, a project-relative path, returns a list
of other files that are compiled along with it."
(if (and (string-match-p "^test/\|^validation-test/" relative-file)
(not (string-match-p "^test/multifile" relative-file)))
nil
(directory-files (concat swift-project-directory (file-name-directory relative-file))))
)

(defun swift-project-swiftc-arguments (relative-file)
"Given RELATIVE-FILE, a project-relative path, returns a list
of arguments that are passed to swiftc when compiling it."
(cond ((string-match-p "^stdlib/public/core/" relative-file)
swift-project-stdlib-swiftc-args)
((string-match-p
"^stdlib/\(public/SwiftOnoneSupport\|internal\|private/SwiftPrivate\(PthreadExtras\|LibcExtras\)?\)/"
relative-file)
swift-project-stdlib-aux-swiftc-args)
(t swift-project-single-frontend-swiftc-args)))

(defun swift-project-swift-syntax-check (swiftc temp-file)
"Return a flymake command-line list for syntax-checking the
current buffer, potentially along with the other .swift and .swift.gyb
files in the same directory."
(let ((project-relative-buffer-file (file-relative-name (buffer-file-name) swift-project-directory)))
(swift-project-gyb-syntax-check1
swiftc temp-file
(swift-project-files-to-compile-with project-relative-buffer-file)
(swift-project-swiftc-arguments project-relative-buffer-file))))

(defun swift-project-gyb-syntax-check1 (swiftc temp-file other-files swiftc-arguments)
"Return a flymake command-line list for syntax-checking the
current buffer along with the other .swift and .swift.gyb
files in the same directory."
(let* (gyb-targets swift-sources)
(dolist (x (cons temp-file other-files))
(unless (file-equal-p x (buffer-file-name))
(when (string-match-p "\\.swift$\\|\\.swift\\.gyb$" (if (string-equal x temp-file) (buffer-file-name) x))
(let ((swift-file (swift-project-gybbed-file x)))
(setq swift-sources (cons swift-file swift-sources))
(when (string-equal "gyb" (file-name-extension x))
(setq gyb-targets (cons swift-file gyb-targets)))))))
(setq swift-sources
(sort swift-sources
(lambda (x y) (< (swift-project-stdlib-compile-order x)
(swift-project-stdlib-compile-order y)))))
`(,(swift-project-executable-find "line-directive")
(,@gyb-targets "--" ,swiftc ,@swiftc-arguments ,@swift-sources))))

(require 'flymake)
(add-to-list 'flymake-allowed-file-name-masks '(".+\\.swift.gyb$" flymake-swift-init))

(provide 'swift-project-settings)
;; end of swift-project-settings.el