diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2032,6 +2032,10 @@ }; for (auto Line : AnnotatedLines) { + if (Line->First && (Line->First->TokenText.startswith("#") || + Line->First->TokenText == "__pragma" || + Line->First->TokenText == "_Pragma")) + continue; for (const FormatToken *FormatTok = Line->First; FormatTok; FormatTok = FormatTok->Next) { if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -16713,6 +16713,15 @@ guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); } +TEST_F(FormatTest, GuessLanguageWithPragmas) { + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "__pragma(warning(disable:))")); + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "#pragma(warning(disable:))")); + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "_Pragma(warning(disable:))")); +} + TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { // ASM symbolic names are identifiers that must be surrounded by [] without // space in between: