[Lldb-commits] [PATCH] D72326: [clang-format] Add option to explicitly specify a config file
zwliew updated this revision to Diff 396578. zwliew added a comment. Herald added subscribers: llvm-commits, openmp-commits, libcxx-commits, lldb-commits, Sanitizers, arjunp, sdasgup3, luke957, asavonic, jeroen.dobbelaere, wenzhicui, wrengr, Chia-hungDuan, armkevincheng, ormris, foad, eric-k256, dcaballe, cota, mravishankar, teijeong, frasercrmck, dexonsmith, rdzhabarov, tatianashp, dang, okura, jdoerfert, msifontes, sstefan1, jurahul, kuter, cmtice, Kayjukh, vkmr, grosul1, martong, Joonsoo, stephenneuendorffer, kerbowa, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, csigg, nicolasvasilache, antiagainst, shauheen, rriddle, mehdi_amini, luismarques, apazos, sameer.abuasal, pengfei, s.egerton, dmgreen, Jim, jocewei, rupprecht, PkmX, arphaman, the_o, brucehoult, MartinMosbeck, rogfer01, steven_wu, edward-jones, zzheng, MaskRay, jrtc27, gbedwell, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, kbarton, aheejin, hiraditya, jgravelle-google, krytarowski, arichardson, sbc100, mgorny, nhaehnle, jvesely, nemanjai, emaste, dylanmckay, dschuff, arsenm. Herald added a reviewer: deadalnix. Herald added a reviewer: andreadb. Herald added a reviewer: lebedev.ri. Herald added a reviewer: jdoerfert. Herald added a reviewer: jhenderson. Herald added a reviewer: jdoerfert. Herald added a reviewer: sstefan1. Herald added a reviewer: nicolasvasilache. Herald added a reviewer: aartbik. Herald added a reviewer: MaskRay. Herald added a reviewer: ftynse. Herald added a reviewer: aaron.ballman. Herald added a reviewer: baziotis. Herald added a reviewer: aartbik. Herald added a reviewer: sjarus. Herald added a reviewer: clementval. Herald added a reviewer: bondhugula. Herald added a reviewer: lebedev.ri. Herald added projects: Sanitizers, LLDB, libc++, OpenMP, MLIR, LLVM, lld-macho. Herald added a reviewer: libc++. Herald added a reviewer: lld-macho. This revision now requires review to proceed. Rebased on master for context Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D72326/new/ https://reviews.llvm.org/D72326 Files: clang/CMakeLists.txt clang/docs/ClangFormat.rst clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/AST/AbstractBasicReader.h clang/include/clang/AST/AbstractBasicWriter.h clang/include/clang/AST/AbstractTypeReader.h clang/include/clang/AST/AbstractTypeWriter.h clang/include/clang/AST/ComputeDependence.h clang/include/clang/AST/CurrentSourceLocExprScope.h clang/include/clang/AST/DeclObjCCommon.h clang/include/clang/AST/FormatString.h clang/include/clang/AST/LexicallyOrderedRecursiveASTVisitor.h clang/include/clang/AST/LocInfoType.h clang/include/clang/AST/NonTrivialTypeVisitor.h clang/include/clang/AST/OSLog.h clang/include/clang/AST/OpenMPClause.h clang/include/clang/AST/QualTypeNames.h clang/include/clang/ASTMatchers/ASTMatchers.h clang/include/clang/ASTMatchers/Dynamic/Diagnostics.h clang/include/clang/ASTMatchers/Dynamic/Parser.h clang/include/clang/ASTMatchers/Dynamic/Registry.h clang/include/clang/ASTMatchers/Dynamic/VariantValue.h clang/include/clang/Analysis/Analyses/ThreadSafetyCommon.h clang/include/clang/Analysis/Analyses/ThreadSafetyUtil.h clang/include/clang/Analysis/AnyCall.h clang/include/clang/Analysis/BodyFarm.h clang/include/clang/Analysis/CloneDetection.h clang/include/clang/Analysis/FlowSensitive/ControlFlowContext.h clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h clang/include/clang/Analysis/FlowSensitive/DataflowWorklist.h clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h clang/include/clang/Analysis/IssueHash.h clang/include/clang/Analysis/PathDiagnostic.h clang/include/clang/Analysis/RetainSummaryManager.h clang/include/clang/Analysis/SelectorExtras.h clang/include/clang/Basic/AlignedAllocation.h clang/include/clang/Basic/AttrDocs.td clang/include/clang/Basic/AttrSubjectMatchRules.h clang/include/clang/Basic/DarwinSDKInfo.h clang/include/clang/Basic/DiagnosticError.h clang/include/clang/Basic/DiagnosticSemaKinds.td clang/include/clang/Basic/OperatorPrecedence.h clang/include/clang/Basic/PragmaKinds.h clang/include/clang/Basic/ProfileList.h clang/include/clang/Basic/TargetID.h clang/include/clang/CodeGen/ObjectFilePCHContainerOperations.h clang/include/clang/Format/Format.h clang/include/clang/Frontend/PCHContainerOperations.h clang/include/clang/Frontend/PrecompiledPreamble.h clang/include/clang/Frontend/SerializedDiagnostics.h clang/include/clang/IndexSerialization/SerializablePathCollection.h clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h clang/include/clang/Lex/PreprocessorExcludedConditionalDirectiveSkipMapping.h clang/include/clang/Parse/RAIIObjectsForParser.h clang/include/clang/Sema/CleanupInfo.h clang/include/clang/Sema/ParsedAttr.h clang/include/clang/Sema/Sema.h clang/include/clang/Sema/SemaConcept.h clang/inc
[Lldb-commits] [PATCH] D72326: [clang-format] Add option to explicitly specify a config file
zwliew added a comment. Herald added a subscriber: JDevlieghere. Oh no...I'm so sorry. Didn't mean to cause this large diff. Trying to fix it now. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D72326/new/ https://reviews.llvm.org/D72326 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [PATCH] D72326: [clang-format] Add option to explicitly specify a config file
zwliew updated this revision to Diff 396579. zwliew added a comment. Fixed rebase and diff. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D72326/new/ https://reviews.llvm.org/D72326 Files: clang/docs/ClangFormat.rst clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp === --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -21584,6 +21584,70 @@ Style.IndentWidth = 7; return Style; }()); + + // Test 9.9: use inheritance from a specific config file. + Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", +"none", "", &FS); + ASSERT_TRUE(static_cast(Style9)); + ASSERT_EQ(*Style9, SubSubStyle); +} + +TEST(FormatStyle, GetStyleOfSpecificFile) { + llvm::vfs::InMemoryFileSystem FS; + // Specify absolute path to a format file in a parent directory. + ASSERT_TRUE( + FS.addFile("/e/.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); + ASSERT_TRUE( + FS.addFile("/e/explicit.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); + ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, + llvm::MemoryBuffer::getMemBuffer("int i;"))); + auto Style = getStyle("file:/e/explicit.clang-format", +"/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); + ASSERT_TRUE(static_cast(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); + + // Specify relative path to a format file. + ASSERT_TRUE( + FS.addFile("../../e/explicit.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); + Style = getStyle("file:../../e/explicit.clang-format", + "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); + ASSERT_TRUE(static_cast(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); + + // Specify path to a format file that does not exist. + Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", + "LLVM", "", &FS); + ASSERT_FALSE(static_cast(Style)); + llvm::consumeError(Style.takeError()); + + // Specify path to a file on the filesystem. + SmallString<128> FormatFilePath; + std::error_code ECF = llvm::sys::fs::createTemporaryFile( + "FormatFileTest", "tpl", FormatFilePath); + EXPECT_FALSE((bool)ECF); + llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); + EXPECT_FALSE((bool)ECF); + FormatFileTest << "BasedOnStyle: Google\n"; + FormatFileTest.close(); + + SmallString<128> TestFilePath; + std::error_code ECT = + llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); + EXPECT_FALSE((bool)ECT); + llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); + CodeFileTest << "int i;\n"; + CodeFileTest.close(); + + std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); + Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); + + llvm::sys::fs::remove(FormatFilePath.c_str()); + llvm::sys::fs::remove(TestFilePath.c_str()); + ASSERT_TRUE(static_cast(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); } TEST_F(ReplacementTest, FormatCodeAfterReplacements) { Index: clang/lib/Format/Format.cpp === --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -3181,6 +3181,8 @@ ".clang-format file located in one of the parent\n" "directories of the source file (or current\n" "directory for stdin).\n" +"Use -style=file: to explicitly specify" +"the configuration file.\n" "Use -style=\"{key: value, ...}\" to set specific\n" "parameters, e.g.:\n" " -style=\"{BasedOnStyle: llvm, IndentWidth: 8}\""; @@ -3233,6 +3235,18 @@ const char *DefaultFallbackStyle = "LLVM"; +llvm::ErrorOr> +loadAndParseConfigFile(StringRef ConfigFile, llvm::vfs::FileSystem *FS, + FormatStyle *Style, bool AllowUnknownOptions) { + llvm::ErrorOr> Text = + FS->getBufferForFile(ConfigFile.str()); + if (auto EC = Text.getError()) +return EC; + if (auto EC = parseConfiguration(*Text.get(), Style, AllowUnknownOptions)) +return EC; + return Text; +} + llvm::Expected getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, llvm::vfs::FileSystem *FS, @@ -3263,6 +3277,28 @@ return Style; } + // User provided clang-format file using -style=file:path/to/format/file. + if (!Style.InheritsParentConfig && + StyleName.startswith_insensitive("file:")) { +auto ConfigFile = StyleName.substr(5); +llvm::ErrorOr> Text = +loadAndParseCon