From a1753f4be12737f36ce9c6eccccddd3be1ec514b Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Fri, 28 Jun 2013 12:51:24 +0000 Subject: Use lexing mode based on FormatStyle.Standard. Summary: Some valid pre-C++11 constructs change meaning when lexed in C++11 mode, e.g. #define x(_a) printf("foo"_a); (example from http://llvm.org/bugs/show_bug.cgi?id=16342). "foo"_a is treated as a user-defined string literal when parsed in C++11 mode. In order to deal with this correctly, we need to set lexing mode according to which standard the code conforms to. We already have a configuration value for this (FormatStyle.Standard), which seems to be appropriate to use in this case as well. Reviewers: klimek CC: cfe-commits, gribozavr Differential Revision: http://llvm-reviews.chandlerc.com/D1028 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185149 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/Format/Format.cpp') diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 766be71bbd..7d6bc14241 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1660,7 +1660,8 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, SourceMgr.overrideFileContents(Entry, Buf); FileID ID = SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); - Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, getFormattingLangOpts()); + Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr, + getFormattingLangOpts(Style.Standard)); SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID); std::vector CharRanges; for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { @@ -1671,10 +1672,10 @@ tooling::Replacements reformat(const FormatStyle &Style, StringRef Code, return reformat(Style, Lex, SourceMgr, CharRanges); } -LangOptions getFormattingLangOpts() { +LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) { LangOptions LangOpts; LangOpts.CPlusPlus = 1; - LangOpts.CPlusPlus11 = 1; + LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1; LangOpts.LineComment = 1; LangOpts.Bool = 1; LangOpts.ObjC1 = 1; -- cgit v1.2.3