Warnings.cpp revision 208954
1//===--- Warnings.cpp - C-Language Front-end ------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Command line warning options handler. 11// 12//===----------------------------------------------------------------------===// 13// 14// This file is responsible for handling all warning options. This includes 15// a number of -Wfoo options and their variants, which are driven by TableGen- 16// generated data, and the special cases -pedantic, -pedantic-errors, -w, 17// -Werror and -Wfatal-errors. 18// 19// Each warning option controls any number of actual warnings. 20// Given a warning option 'foo', the following are valid: 21// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo 22// 23#include "clang/Frontend/Utils.h" 24#include "clang/Basic/Diagnostic.h" 25#include "clang/Sema/SemaDiagnostic.h" 26#include "clang/Lex/LexDiagnostic.h" 27#include "clang/Frontend/DiagnosticOptions.h" 28#include "clang/Frontend/FrontendDiagnostic.h" 29#include <cstring> 30#include <utility> 31#include <algorithm> 32using namespace clang; 33 34void clang::ProcessWarningOptions(Diagnostic &Diags, 35 const DiagnosticOptions &Opts) { 36 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers 37 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); 38 39 // Handle -ferror-limit 40 if (Opts.ErrorLimit) 41 Diags.setErrorLimit(Opts.ErrorLimit); 42 if (Opts.TemplateBacktraceLimit) 43 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); 44 45 // If -pedantic or -pedantic-errors was specified, then we want to map all 46 // extension diagnostics onto WARNING or ERROR unless the user has futz'd 47 // around with them explicitly. 48 if (Opts.PedanticErrors) 49 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error); 50 else if (Opts.Pedantic) 51 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn); 52 else 53 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore); 54 55 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { 56 const std::string &Opt = Opts.Warnings[i]; 57 const char *OptStart = &Opt[0]; 58 const char *OptEnd = OptStart+Opt.size(); 59 assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); 60 61 // Check to see if this warning starts with "no-", if so, this is a negative 62 // form of the option. 63 bool isPositive = true; 64 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { 65 isPositive = false; 66 OptStart += 3; 67 } 68 69 // Figure out how this option affects the warning. If -Wfoo, map the 70 // diagnostic to a warning, if -Wno-foo, map it to ignore. 71 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; 72 73 // -Wsystem-headers is a special case, not driven by the option table. It 74 // cannot be controlled with -Werror. 75 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { 76 Diags.setSuppressSystemWarnings(!isPositive); 77 continue; 78 } 79 80 // -Werror/-Wno-error is a special case, not controlled by the option table. 81 // It also has the "specifier" form of -Werror=foo and -Werror-foo. 82 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { 83 const char *Specifier = 0; 84 if (OptEnd-OptStart != 5) { // Specifier must be present. 85 if ((OptStart[5] != '=' && OptStart[5] != '-') || 86 OptEnd-OptStart == 6) { 87 Diags.Report(diag::warn_unknown_warning_specifier) 88 << "-Werror" << ("-W" + Opt); 89 continue; 90 } 91 Specifier = OptStart+6; 92 } 93 94 if (Specifier == 0) { 95 Diags.setWarningsAsErrors(isPositive); 96 continue; 97 } 98 99 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning. 100 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR; 101 OptStart = Specifier; 102 } 103 104 // -Wfatal-errors is yet another special case. 105 if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) { 106 const char* Specifier = 0; 107 if (OptEnd-OptStart != 12) { 108 if ((OptStart[12] != '=' && OptStart[12] != '-') || 109 OptEnd-OptStart == 13) { 110 Diags.Report(diag::warn_unknown_warning_specifier) 111 << "-Wfatal-errors" << ("-W" + Opt); 112 continue; 113 } 114 Specifier = OptStart + 13; 115 } 116 117 if (Specifier == 0) { 118 Diags.setErrorsAsFatal(isPositive); 119 continue; 120 } 121 122 // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo 123 // maps it to Error. 124 Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL; 125 OptStart = Specifier; 126 } 127 128 if (Diags.setDiagnosticGroupMapping(OptStart, Mapping)) 129 Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt); 130 } 131} 132