From: Chris Lattner Date: Tue, 3 Nov 2009 19:50:27 +0000 (+0000) Subject: Implement support for the -undef command line option, patch by X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e6113de52df132b89c3a5a6141f17d37e83322ae;p=clang Implement support for the -undef command line option, patch by Roman Divacky! PR5363 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85932 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/InitPreprocessor.h b/include/clang/Frontend/InitPreprocessor.h index eb03602dc5..b29ee2728a 100644 --- a/include/clang/Frontend/InitPreprocessor.h +++ b/include/clang/Frontend/InitPreprocessor.h @@ -63,7 +63,8 @@ public: /// environment ready to process a single file. This returns true on error. /// bool InitializePreprocessor(Preprocessor &PP, - const PreprocessorInitOptions& InitOptions); + const PreprocessorInitOptions& InitOptions, + bool undef_macros); } // end namespace clang diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 47234ef649..521c90dc31 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1015,6 +1015,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, II.getInputArg().renderAsInput(Args, CmdArgs); } + Args.AddAllArgs(CmdArgs, options::OPT_undef); + const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(C, "clang-cc")); Dest.addCommand(new Command(JA, Exec, CmdArgs)); diff --git a/lib/Frontend/InitPreprocessor.cpp b/lib/Frontend/InitPreprocessor.cpp index 2719fbb456..ec5c1061bb 100644 --- a/lib/Frontend/InitPreprocessor.cpp +++ b/lib/Frontend/InitPreprocessor.cpp @@ -443,7 +443,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, /// environment ready to process a single file. This returns true on error. /// bool clang::InitializePreprocessor(Preprocessor &PP, - const PreprocessorInitOptions &InitOpts) { + const PreprocessorInitOptions &InitOpts, + bool undef_macros) { std::vector PredefineBuffer; const char *LineDirective = "# 1 \"\" 3\n"; @@ -451,8 +452,9 @@ bool clang::InitializePreprocessor(Preprocessor &PP, LineDirective, LineDirective+strlen(LineDirective)); // Install things like __POWERPC__, __GNUC__, etc into the macro table. - InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), - PredefineBuffer); + if (!undef_macros) + InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), + PredefineBuffer); // Add on the predefines from the driver. Wrap in a #line directive to report // that they come from the command line. diff --git a/test/Preprocessor/macro_undef.c b/test/Preprocessor/macro_undef.c new file mode 100644 index 0000000000..4507cddcb7 --- /dev/null +++ b/test/Preprocessor/macro_undef.c @@ -0,0 +1,4 @@ +// RUN: clang-cc -dM -undef -Dfoo=1 -E %s | FileCheck %s + +// CHECK-NOT: #define __clang__ +// CHECK: #define foo 1 diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 5840f5f990..71e5469800 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -932,6 +932,9 @@ static bool InitializeSourceManager(Preprocessor &PP, // -A... - Play with #assertions // -undef - Undefine all predefined macros +static llvm::cl::opt +undef_macros("undef", llvm::cl::value_desc("macro"), llvm::cl::desc("undef all system defines")); + static llvm::cl::list D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, llvm::cl::desc("Predefine the specified macro")); @@ -1243,7 +1246,7 @@ public: PreprocessorInitOptions InitOpts; InitializePreprocessorInitOptions(InitOpts); - if (InitializePreprocessor(*PP, InitOpts)) + if (InitializePreprocessor(*PP, InitOpts, undef_macros)) return 0; return PP.take();