@@ -424,6 +424,36 @@ std::string CppCheck::getLibraryDumpData() const {
424424 return out;
425425}
426426
427+ std::string CppCheck::getClangFlags (Standards::Language fileLang) const {
428+ std::string flags;
429+
430+ const Standards::Language lang = mSettings .enforcedLang != Standards::None ? mSettings .enforcedLang : fileLang;
431+
432+ switch (lang) {
433+ case Standards::Language::None:
434+ case Standards::Language::C:
435+ flags = " -x c " ;
436+ if (!mSettings .standards .stdValueC .empty ())
437+ flags += " -std=" + mSettings .standards .stdValueC + " " ;
438+ break ;
439+ case Standards::Language::CPP:
440+ flags += " -x c++ " ;
441+ if (!mSettings .standards .stdValueCPP .empty ())
442+ flags += " -std=" + mSettings .standards .stdValueCPP + " " ;
443+ break ;
444+ }
445+
446+ for (const std::string &i: mSettings .includePaths )
447+ flags += " -I" + i + " " ;
448+
449+ flags += getDefinesFlags (mSettings .userDefines );
450+
451+ for (const std::string &i: mSettings .userIncludes )
452+ flags += " --include " + cmdFileName (i) + " " ;
453+
454+ return flags;
455+ }
456+
427457unsigned int CppCheck::checkClang (const FileWithDetails &file)
428458{
429459 if (mSettings .checks .isEnabled (Checks::unusedFunction) && !mUnusedFunctionsCheck )
@@ -433,12 +463,6 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
433463 mErrorLogger .reportOut (std::string (" Checking " ) + file.spath () + " ..." , Color::FgGreen);
434464
435465 // TODO: get language from FileWithDetails object
436- bool isCpp;
437- if (mSettings .enforcedLang != Standards::None)
438- isCpp = (mSettings .enforcedLang == Standards::CPP);
439- else
440- isCpp = Path::identify (file.spath (), mSettings .cppHeaderProbe ) == Standards::Language::CPP;
441- const std::string langOpt = isCpp ? " -x c++" : " -x c" ;
442466 const std::string analyzerInfo = mSettings .buildDir .empty () ? std::string () : AnalyzerInformation::getAnalyzerInfoFile (mSettings .buildDir , file.spath (), emptyString);
443467 const std::string clangcmd = analyzerInfo + " .clang-cmd" ;
444468 const std::string clangStderr = analyzerInfo + " .clang-stderr" ;
@@ -451,18 +475,9 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
451475 }
452476#endif
453477
454- std::string flags (langOpt + " " );
455- if (isCpp && !mSettings .standards .stdValueCPP .empty ())
456- flags += " -std=" + mSettings .standards .stdValueCPP + " " ;
457- if (!isCpp && !mSettings .standards .stdValueC .empty ())
458- flags += " -std=" + mSettings .standards .stdValueC + " " ;
459-
460- for (const std::string &i: mSettings .includePaths )
461- flags += " -I" + i + " " ;
462-
463- flags += getDefinesFlags (mSettings .userDefines );
464-
465- const std::string args2 = " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + file.spath ();
478+ const std::string args2 = " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " +
479+ getClangFlags (Path::identify (file.spath (), mSettings .cppHeaderProbe )) +
480+ file.spath ();
466481 const std::string redirect2 = analyzerInfo.empty () ? std::string (" 2>&1" ) : (" 2> " + clangStderr);
467482 if (!mSettings .buildDir .empty ()) {
468483 std::ofstream fout (clangcmd);
0 commit comments