Skip to content

Commit 954f083

Browse files
author
Dave Abrahams
authored
Merge pull request swiftlang#5219 from apple/flymake
[emacs-support] More flymake support
2 parents fd47f92 + 61f0245 commit 954f083

File tree

3 files changed

+202
-24
lines changed

3 files changed

+202
-24
lines changed

.dir-locals.el

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,19 @@
1010
;
1111
;===----------------------------------------------------------------------===;
1212
;;; Directory Local Variables
13-
;;; See Info node `(emacs) Directory Variables' for more information.
13+
;;; For more information see (info "(emacs) Directory Variables")
1414

1515
((nil
1616
(tab-width . 2)
1717
(fill-column . 80)
18-
(eval .
19-
;; Load the Swift project's settings. To suppress this action
20-
;; you can put "(provide 'swift-project-settings)" in your
21-
;; .emacs
18+
(eval let* ((x (dir-locals-find-file default-directory))
19+
(this-directory (if (listp x) (car x)
20+
(file-name-directory x))))
2221
(unless (featurep 'swift-project-settings)
23-
;; Make sure the project's own utils directory is in the
24-
;; load path, but don't override any one the user might have
25-
;; set up.
26-
(add-to-list
27-
'load-path
28-
(concat
29-
(let ((dlff (dir-locals-find-file default-directory)))
30-
(if (listp dlff) (car dlff) (file-name-directory dlff)))
31-
"utils")
32-
:append)
33-
;; Load our project's settings -- indirectly brings in swift-mode
22+
(add-to-list 'load-path (concat this-directory "utils") :append)
23+
(let ((swift-project-directory this-directory))
3424
(require 'swift-project-settings)))
25+
(set (make-local-variable 'swift-project-directory) this-directory))
3526
(c-file-style . "swift")
3627
)
3728
(c++-mode
@@ -44,6 +35,8 @@
4435
(whitespace-style . (face lines indentation:space))
4536
(eval . (whitespace-mode)))
4637
(swift-mode
38+
(swift-find-executable-fn . swift-project-executable-find)
39+
(swift-syntax-check-fn . swift-project-swift-syntax-check)
4740
(whitespace-style . (face lines indentation:space))
4841
(eval . (whitespace-mode))
4942
(swift-basic-offset . 2)

utils/swift-mode.el

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -410,20 +410,35 @@ Use `M-x hs-show-all' to show them again."
410410

411411
(require 'flymake)
412412

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

420-
(defvar swift-syntax-check-function 'swift-syntax-check-directory
421+
(defvar-local swift-syntax-check-fn 'swift-syntax-check-directory
421422
"Function to create the swift command-line that syntax-checks the current buffer.
422423
The function is called with two arguments, the swiftc executable, and
423424
the name of a temporary file that will contain the contents of the
424425
current buffer.
425426
Set to 'swift-syntax-check-single-file to ignore other files in the current directory.")
426-
(make-variable-buffer-local 'swift-syntax-check-function)
427+
(put 'swift-syntax-check-fn 'safe-local-variable 'functionp)
428+
429+
(defvar-local swift-syntax-check-args '("-parse")
430+
"List of arguments to be passed to swiftc for syntax checking.
431+
Elements of this list that are strings are inserted literally
432+
into the command line. Elements that are S-expressions are
433+
evaluated. The resulting list is cached in a file-local
434+
variable, `swift-syntax-check-evaluated-args', so if you change
435+
this variable you should set that one to nil.")
436+
(put 'swift-syntax-check-args 'safe-local-variable 'listp)
437+
438+
(defvar-local swift-syntax-check-evaluated-args
439+
"File-local cache of swift arguments used for syntax checking
440+
variable, `swift-syntax-check-args', so if you change
441+
that variable you should set this one to nil.")
427442

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

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

utils/swift-project-settings.el

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
(append '(("\\.swift$" . swift-mode) ("\\.gyb$" python-mode t)) auto-mode-alist))
2323

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

258+
(defvar swift-project-directory
259+
(file-name-directory (directory-file-name (file-name-directory load-file-name)))
260+
"Directory where the swift project containing this file is located.
261+
Defaults to the parent directory of `swift-project-settings.el'.
262+
The setting for file-local values of this variable comes from
263+
.dir-locals.el in the project's root directory")
264+
(put 'swift-project-directory 'safe-local-variable 'stringp)
265+
266+
(defun swift-project-default-build-directory (project-directory)
267+
"Returns the default build directory given a project directory name, `DIR/../build'"
268+
(concat (file-name-directory (directory-file-name project-directory)) "build/"))
269+
270+
;; This name doesn't end in "function" to avoid being unconditionally marked as risky.
271+
(defcustom swift-project-build-directory-fn 'swift-project-default-build-directory
272+
"A function that, given a swift project directory name,
273+
computes the directory where your build leaves build products.
274+
Flymake support may search here for a swift compiler to use, for example.
275+
Defaults to `(concat swift-project-directory \"../build\")'."
276+
:type 'function
277+
)
278+
279+
(defun swift-project-executable-find (command)
280+
"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.
281+
Given an absolute path, returns it verbatim. This is a pretty
282+
good heuristic for locating things to use when working on swift
283+
itself, and is used as the value of swift-find-executable-fn"
284+
(if (file-name-absolute-p command) command
285+
(let* ((utility (concat swift-project-directory "utils/" command))
286+
(newest (and (file-executable-p utility) utility)))
287+
(dolist (x (file-expand-wildcards
288+
(concat (funcall swift-project-build-directory-fn swift-project-directory)
289+
"*/swift-*/bin/" command)))
290+
(when (and (file-executable-p x) (or (null newest) (file-newer-than-file-p x newest)))
291+
(setq newest x)))
292+
(or newest (executable-find command)))))
293+
294+
(defvar swift-project-sdk-path
295+
(substring (shell-command-to-string "xcrun --show-sdk-path") 0 -1)
296+
"The path to the swift SDK to use for syntax checking, etc.")
297+
298+
(defvar swift-project--gyb-temp-file-directory nil)
299+
(defun swift-project-gyb-temp-file-directory ()
300+
"A directory used for gyb-processed files"
301+
(or swift-project--gyb-temp-file-directory
302+
(setq swift-project--gyb-temp-file-directory
303+
(make-temp-file "swift-project-gyb" :DIRECTORY))))
304+
305+
(defun swift-project-gyb-output-file-name (input-file-name)
306+
"Given the name of a .gyb file, return the name of the temporary file we'll use for its expanded result."
307+
(file-name-sans-extension
308+
(expand-file-name
309+
(subst-char-in-string
310+
?/ ?! (replace-regexp-in-string
311+
"!" "!!"
312+
(if (file-name-absolute-p input-file-name) input-file-name
313+
(expand-file-name input-file-name))))
314+
(swift-project-gyb-temp-file-directory))))
315+
316+
(defun swift-project-gybbed-file (input-file-name)
317+
"Given the name of a .gyb file, process it with gyb and return an output file name.
318+
Given any other file name, just return that name."
319+
(if (not (string-equal (file-name-extension input-file-name) "gyb"))
320+
input-file-name
321+
(let ((result (swift-project-gyb-output-file-name input-file-name)))
322+
(prog1 result
323+
(unless (file-newer-than-file-p result input-file-name)
324+
(with-temp-buffer
325+
(let* ((gyb (swift-project-executable-find "gyb"))
326+
(status (call-process gyb nil t nil "-DCMAKE_SIZEOF_VOID_P=8" input-file-name)))
327+
(unless (eq status 0)
328+
(error "%s exited with status %s" gyb status)))
329+
;; Use write-region instead of write-file to avoid spewing messages.
330+
(write-region nil nil result nil 566)))))))
331+
332+
(defconst swift-project-stdlib-compile-order
333+
"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"
334+
335+
"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."
336+
)
337+
338+
(defun swift-project-stdlib-compile-order (filename)
339+
"Return an integer representing where in the required
340+
compilation order the given file should appear."
341+
(save-match-data
342+
(if (string-match
343+
(concat "\\<" (regexp-quote (replace-regexp-in-string "^\\(?:.*[/!]\\)?\\([^.]*\\).*" "\\1" filename)) "\\>")
344+
swift-project-stdlib-compile-order)
345+
(match-beginning 0) 0)))
346+
347+
(defconst swift-project-common-swiftc-args
348+
(list "-parse" "-sdk" swift-project-sdk-path
349+
"-F" (concat (file-name-as-directory swift-project-sdk-path) "../../../Developer/Library/Frameworks")
350+
"-D" "INTERNAL_CHECKS_ENABLED"
351+
"-no-link-objc-runtime")
352+
"The common arguments we'll pass to swiftc for syntax-checking
353+
everything in the Swift project" )
354+
355+
(defconst swift-project-single-frontend-swiftc-args
356+
(append swift-project-common-swiftc-args
357+
(list "-force-single-frontend-invocation" "-parse-as-library"))
358+
"The arguments we'll pass to swiftc for syntax-checking
359+
libraries that require a single frontend invocation" )
360+
361+
(defconst swift-project-stdlib-aux-swiftc-args
362+
(append swift-project-single-frontend-swiftc-args
363+
(list "-Xfrontend" "-sil-serialize-all" "-parse-stdlib"))
364+
"swiftc arguments for library components that are compiled as
365+
though they are part of the standard library even though
366+
they're not strictly in that binary." )
367+
368+
(defconst swift-project-stdlib-swiftc-args
369+
(append
370+
swift-project-stdlib-aux-swiftc-args (list "-nostdimport" "-module-name" "Swift"))
371+
"The arguments we'll pass to swiftc for syntax-checking the
372+
standard library" )
373+
374+
(defun swift-project-files-to-compile-with (relative-file)
375+
"Given RELATIVE-FILE, a project-relative path, returns a list
376+
of other files that are compiled along with it."
377+
(if (and (string-match-p "^test/\|^validation-test/" relative-file)
378+
(not (string-match-p "^test/multifile" relative-file)))
379+
nil
380+
(directory-files (concat swift-project-directory (file-name-directory relative-file))))
381+
)
382+
383+
(defun swift-project-swiftc-arguments (relative-file)
384+
"Given RELATIVE-FILE, a project-relative path, returns a list
385+
of arguments that are passed to swiftc when compiling it."
386+
(cond ((string-match-p "^stdlib/public/core/" relative-file)
387+
swift-project-stdlib-swiftc-args)
388+
((string-match-p
389+
"^stdlib/\(public/SwiftOnoneSupport\|internal\|private/SwiftPrivate\(PthreadExtras\|LibcExtras\)?\)/"
390+
relative-file)
391+
swift-project-stdlib-aux-swiftc-args)
392+
(t swift-project-single-frontend-swiftc-args)))
393+
394+
(defun swift-project-swift-syntax-check (swiftc temp-file)
395+
"Return a flymake command-line list for syntax-checking the
396+
current buffer, potentially along with the other .swift and .swift.gyb
397+
files in the same directory."
398+
(let ((project-relative-buffer-file (file-relative-name (buffer-file-name) swift-project-directory)))
399+
(swift-project-gyb-syntax-check1
400+
swiftc temp-file
401+
(swift-project-files-to-compile-with project-relative-buffer-file)
402+
(swift-project-swiftc-arguments project-relative-buffer-file))))
403+
404+
(defun swift-project-gyb-syntax-check1 (swiftc temp-file other-files swiftc-arguments)
405+
"Return a flymake command-line list for syntax-checking the
406+
current buffer along with the other .swift and .swift.gyb
407+
files in the same directory."
408+
(let* (gyb-targets swift-sources)
409+
(dolist (x (cons temp-file other-files))
410+
(unless (file-equal-p x (buffer-file-name))
411+
(when (string-match-p "\\.swift$\\|\\.swift\\.gyb$" (if (string-equal x temp-file) (buffer-file-name) x))
412+
(let ((swift-file (swift-project-gybbed-file x)))
413+
(setq swift-sources (cons swift-file swift-sources))
414+
(when (string-equal "gyb" (file-name-extension x))
415+
(setq gyb-targets (cons swift-file gyb-targets)))))))
416+
(setq swift-sources
417+
(sort swift-sources
418+
(lambda (x y) (< (swift-project-stdlib-compile-order x)
419+
(swift-project-stdlib-compile-order y)))))
420+
`(,(swift-project-executable-find "line-directive")
421+
(,@gyb-targets "--" ,swiftc ,@swiftc-arguments ,@swift-sources))))
422+
423+
(require 'flymake)
424+
(add-to-list 'flymake-allowed-file-name-masks '(".+\\.swift.gyb$" flymake-swift-init))
425+
258426
(provide 'swift-project-settings)
259427
;; end of swift-project-settings.el

0 commit comments

Comments
 (0)