10

I'm using flycheck for C++ development, and get the following warning:

#pragma once in main file

In all headers included in a main file. Searching the webs, I get the impression this has something to do with how gcc and clang (both give the same warning) compile for flymake.

I would like to get rid of this warning. Is there anything I can do?

erikstokes
  • 12,686
  • 2
  • 34
  • 56
Spacemoose
  • 877
  • 1
  • 7
  • 18
  • It will be easier to figure out how to _suppress_ this warning if we can _reproduce_ it first. Can you give an example file or files that exhibit the problem, while still being as simple as you can make them? – Ben Liblit Oct 31 '15 at 20:36
  • I have a question similar to yours, I have [an answer](https://emacs.stackexchange.com/a/39134/18592). –  Feb 27 '18 at 23:43

2 Answers2

2

Clang has an option to disable this warning. Adding the following to my config fixed the issue for me.

(with-eval-after-load "flycheck"
    (setq flycheck-clang-warnings `(,@flycheck-clang-warnings
                                    "no-pragma-once-outside-header")))

Unfortunately, I don't know if similar option exists for gcc.

grepcake
  • 145
  • 7
0

Currently GCC has no way of suppressing this warning. And flycheck has no way to ignore errors without touching its internals.

Here is a hack that works for gcc:

; ignore gcc "#pragma once" warning
(with-eval-after-load "flycheck"
  (eval-when-compile (require 'flycheck))  ; for flycheck-error struct
  (defun my-filter-pragma-once-in-main (orig-fun errors)
    (dolist (err errors)
      (-when-let (msg (flycheck-error-message err))
        (setf (flycheck-error-message err)
              (if (string-match-p "#pragma once in main file" msg) nil msg))))
    (funcall orig-fun errors))
  (advice-add 'flycheck-sanitize-errors :around #'my-filter-pragma-once-in-main))

For completeness, @grepcake's answer for clang:

; ignore clang "#pragma once" warning
(with-eval-after-load "flycheck"
  (setq flycheck-clang-warnings `(,@flycheck-clang-warnings
                                  "no-pragma-once-outside-header")))
maxy
  • 101
  • 2