-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi,
(resend to cfe-commits instead of cfe-dev)

please see my feature proposal for an option -fallback-style=fail below.

Thanks in advance for feedback,
Paul

- -------- Forwarded Message --------
Subject: [cfe-dev] [clang-format] add option -fallback-style=fail (fail if 
-style=file falls back)
Date: Thu, 26 Jan 2017 16:05:32 +0100
From: Paul Seyfert via cfe-dev <cfe-...@lists.llvm.org>
Reply-To: Paul Seyfert <pseyf...@mathphys.fsk.uni-heidelberg.de>
To: cfe-...@lists.llvm.org

Dear all,

I want to provide information to the clang-format user if the style
is/can be used, or if clang-format sees itself forced to fall back to
the fallback-style. I.e. in contrast to doing nothing, as with
- -fallback-style=none, I want clang-format to fail. Kind of as a reminder
to the user "hey, maybe you forgot to add your .clang-format file to
your project".

I was thinking about doing this through an option
`-fallback-style=fail`. Do you think that is the right approach?

If yes, I gave it a try and wrote the attached one-off patch, which adds
the option `-fallback-style=fail`.

I *briefly* tested it on my system by building `make clang-format`[1] and
tried what happens when no .clang-format file exists and I run
`clang-format -style=file -fallback-style=fail main.cc` on a minimal
hello world c++ file.

NB: the patch does contain an update of the unit test, but I did not
manage to run it (problems with the system I was running on …).

Thanks in advance for feedback,

Paul



For reference:
This addresses what I asked on Stackoverflow
http://stackoverflow.com/questions/41758865/determine-if-clang-format-finds-clang-format-file-in-a-shell-or-cmake-script

and is a duplication of my pull request on github
https://github.com/llvm-mirror/clang/pull/21

[1]: when following steps 1-3 from
https://clang.llvm.org/get_started.html and then running `make
clang-format` in step 7 instead of `make`.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJYkJhtAAoJEPOmP9OjPXmrVs8QAJKM1RWO2f+w2OOe4EckyPcK
093d7zwr37r21TFGVzLh76JAhhswahB93l4lugoYVDl4n9L2TF5mi/7SMapkxmeo
3VnNN+VlyuCDzIo0Mj8tFCPgTled6TDNKmXbZkxabJ3nyylJXdRAeo4sP1saf5Me
nMVhZPGeOX6VP+9JPuLIs+8qcEFyz90cZIBXUMtxG045ugVQ+DRcrNUAxRSg/c9s
0XL7oCO8D2QDuPdyun9Fe7Btdq+gy8cC32oWkS5rzniY9nfZKJIS1GPzmhJ2YW/w
/moFP4kcrRAKHWDr2CLTAOKS8W3qZzi5RLiCHhHt2Cm5ASsOlzsbf3fz4uCeqbvm
uCFXn7OglJagxpOvXf6oGQKbbLSPZ26BC6eQav3AB4rKlbFNn96B1GxUY3U3IXb4
lhvOyL3AcHS+juxPOnUoRL9cChB2sVPc7ve9Z+yWw6nHwVbG/AnEIYq9BGb9sp5d
9J04XwrjDtx8VU0zjzJTBXxPdeS3aDh+PWcHbe17l2+lKHEFA/Ch+/b0Svbvtmem
DEG1K2cvOSn71cG4fFTl9DR++nR93frij2te5NGHpqDTbQoVb45RhpBViyf5Qhvu
DT/HZHjeQ+FEjnsikgE1q9VJZhp2gUBKssrLBRRavUz5FAe2q/vzk09oiQaWUODU
Iwl0nEJ1u/KQzVfv+yb3
=O68y
-----END PGP SIGNATURE-----
Index: docs/ClangFormat.rst
===================================================================
--- docs/ClangFormat.rst	(Revision 293072)
+++ docs/ClangFormat.rst	(Arbeitskopie)
@@ -42,6 +42,7 @@
                                 -style=file, but can not find the .clang-format
                                 file to use.
                                 Use -fallback-style=none to skip formatting.
+                                Use -fallback-style=fail to exit with an error.
     -i                        - Inplace edit <file>s, if specified.
     -length=<uint>            - Format a range of this length (in bytes).
                                 Multiple ranges can be formatted by specifying
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp	(Revision 293072)
+++ lib/Format/Format.cpp	(Arbeitskopie)
@@ -737,7 +737,7 @@
     *Style = getWebKitStyle();
   } else if (Name.equals_lower("gnu")) {
     *Style = getGNUStyle();
-  } else if (Name.equals_lower("none")) {
+  } else if (Name.equals_lower("none") || Name.equals_lower("fail")) {
     *Style = getNoStyle();
   } else {
     return false;
@@ -1977,7 +1977,12 @@
     return make_string_error("Configuration file(s) do(es) not support " +
                              getLanguageName(Style.Language) + ": " +
                              UnsuitableConfigFiles);
-  return FallbackStyle;
+
+  if (!FallbackStyleName.equals_lower("fail")) {
+    return FallbackStyle;
+  } else {
+    return make_string_error("Configuration file not found.");
+  }
 }
 
 } // namespace format
Index: tools/clang-format/ClangFormat.cpp
===================================================================
--- tools/clang-format/ClangFormat.cpp	(Revision 293072)
+++ tools/clang-format/ClangFormat.cpp	(Arbeitskopie)
@@ -68,7 +68,8 @@
                        "fallback in case clang-format is invoked with\n"
                        "-style=file, but can not find the .clang-format\n"
                        "file to use.\n"
-                       "Use -fallback-style=none to skip formatting."),
+                       "Use -fallback-style=none to skip formatting.\n"
+                       "Use -fallback-style=fail to exit with an error."),
               cl::init("LLVM"), cl::cat(ClangFormatCategory));
 
 static cl::opt<std::string>
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp	(Revision 293072)
+++ unittests/Format/FormatTest.cpp	(Arbeitskopie)
@@ -11465,7 +11465,12 @@
   ASSERT_TRUE((bool)Style2);
   ASSERT_EQ(*Style2, getNoStyle());
 
-  // Test 2.3: format if config is found with no based style while fallback is
+  // Test 2.3: no format on 'fail' fallback style.
+  Style2 = getStyle("file", "/b/test.cpp", "fail", "", &FS);
+  ASSERT_FALSE((bool)Style2);
+  llvm::consumeError(Style2.takeError());
+
+  // Test 2.4: format if config is found with no based style while fallback is
   // 'none'.
   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
@@ -11473,7 +11478,7 @@
   ASSERT_TRUE((bool)Style2);
   ASSERT_EQ(*Style2, getLLVMStyle());
 
-  // Test 2.4: format if yaml with no based style, while fallback is 'none'.
+  // Test 2.5: format if yaml with no based style, while fallback is 'none'.
   Style2 = getStyle("{}", "a.h", "none", "", &FS);
   ASSERT_TRUE((bool)Style2);
   ASSERT_EQ(*Style2, getLLVMStyle());

_______________________________________________
cfe-dev mailing list
cfe-...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to