From 88dc74929ad660e2a69bc0d720de5e36fc83f7e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 27 Mar 2019 10:23:04 +0100 Subject: [PATCH] Add defines set by compiler options when using compilation database (#1763) * Add defines set by compiler options when using compilation database sets __cplusplus and __STDC_VERSION__ based on -std and the defines for -municode, -fpie, -fPIE, -fpic and -fPIC * Fixed merge --- lib/importproject.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 6f6e37f94..601c1475a 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -246,11 +246,86 @@ void ImportProject::FileSettings::parseCommand(const std::string &command) ++pos; const std::string stdval = readUntil(command, &pos, " "); standard = stdval; + if (standard.compare(0, 3, "c++") || standard.compare(0, 5, "gnu++")) { + std::string stddef; + if (standard == "c++98" || standard == "gnu++98" || standard == "c++03" || standard == "gnu++03") { + stddef = "199711L"; + } + else if (standard == "c++11" || standard == "gnu++11"|| standard == "c++0x" || standard == "gnu++0x") { + stddef = "201103L"; + } + else if (standard == "c++14" || standard == "gnu++14" || standard == "c++1y" || standard == "gnu++1y") { + stddef = "201402L"; + } + else if (standard == "c++17" || standard == "gnu++17" || standard == "c++1z" || standard == "gnu++1z") { + stddef = "201703L"; + } + + if (stddef.empty()) { + // TODO: log error + continue; + } + + defs += "__cplusplus="; + defs += stddef; + defs += ";"; + } + else if (standard.compare(0, 1, "c") || standard.compare(0, 3, "gnu")) { + if (standard == "c90" || standard == "iso9899:1990" || standard == "gnu90" || standard == "iso9899:199409") { + // __STDC_VERSION__ is not set for C90 although the macro was added in the 1994 amendments + continue; + } + + std::string stddef; + + if (standard == "c99" || standard == "iso9899:1999" || standard == "gnu99") { + stddef = "199901L"; + } + else if (standard == "c11" || standard == "iso9899:2011" || standard == "gnu11" || standard == "c1x" || standard == "gnu1x") { + stddef = "201112L"; + } + else if (standard == "c17") { + stddef = "201710L"; + } + + if (stddef.empty()) { + // TODO: log error + continue; + } + + defs += "__STDC_VERSION__="; + defs += stddef; + defs += ";"; + } } else if (F == 'i' && fval == "system") { ++pos; const std::string isystem = readUntil(command, &pos, " "); systemIncludePaths.push_back(isystem); } + else if (F=='m') { + if (fval == "unicode") { + defs += "UNICODE"; + defs += ";"; + } + } + else if (F=='f') { + if (fval == "pic") { + defs += "__pic__"; + defs += ";"; + } + else if (fval == "PIC") { + defs += "__PIC__"; + defs += ";"; + } + else if (fval == "pie") { + defs += "__pie__"; + defs += ";"; + } + else if (fval == "PIE") { + defs += "__PIE__"; + defs += ";"; + } + } } setDefines(defs); } @@ -289,7 +364,7 @@ void ImportProject::importCompileCommands(std::istream &istr) path += file; fs.filename = Path::simplifyPath(path); } - fs.parseCommand(command); // read settings; -D, -I, -U, -std + fs.parseCommand(command); // read settings; -D, -I, -U, -std, -m*, -f* std::map variables; fs.setIncludePaths(directory, fs.includePaths, variables); fileSettings.push_back(fs);