Warnings.cpp revision 194711
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 and 17// -Werror. 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 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/FrontendDiagnostic.h" 28#include <cstdio> 29#include <cstring> 30#include <utility> 31#include <algorithm> 32using namespace clang; 33 34bool clang::ProcessWarningOptions(Diagnostic &Diags, 35 std::vector<std::string> &Warnings, 36 bool Pedantic, bool PedanticErrors, 37 bool NoWarnings) { 38 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers 39 Diags.setIgnoreAllWarnings(NoWarnings); 40 41 // If -pedantic or -pedantic-errors was specified, then we want to map all 42 // extension diagnostics onto WARNING or ERROR unless the user has futz'd 43 // around with them explicitly. 44 if (PedanticErrors) 45 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error); 46 else if (Pedantic) 47 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn); 48 else 49 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore); 50 51 // FIXME: -Wfatal-errors / -Wfatal-errors=foo 52 53 for (unsigned i = 0, e = Warnings.size(); i != e; ++i) { 54 const std::string &Opt = Warnings[i]; 55 const char *OptStart = &Opt[0]; 56 const char *OptEnd = OptStart+Opt.size(); 57 assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); 58 59 // Check to see if this warning starts with "no-", if so, this is a negative 60 // form of the option. 61 bool isPositive = true; 62 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { 63 isPositive = false; 64 OptStart += 3; 65 } 66 67 // Figure out how this option affects the warning. If -Wfoo, map the 68 // diagnostic to a warning, if -Wno-foo, map it to ignore. 69 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; 70 71 // -Wsystem-headers is a special case, not driven by the option table. It 72 // cannot be controlled with -Werror. 73 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { 74 Diags.setSuppressSystemWarnings(!isPositive); 75 continue; 76 } 77 78 // -Werror/-Wno-error is a special case, not controlled by the option table. 79 // It also has the "specifier" form of -Werror=foo and -Werror-foo. 80 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { 81 const char *Specifier = 0; 82 if (OptEnd-OptStart != 5) { // Specifier must be present. 83 if ((OptStart[5] != '=' && OptStart[5] != '-') || 84 OptEnd-OptStart == 6) { 85 fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n", 86 Opt.c_str()); 87 continue; 88 } 89 Specifier = OptStart+6; 90 } 91 92 if (Specifier == 0) { 93 Diags.setWarningsAsErrors(true); 94 continue; 95 } 96 97 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning. 98 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR; 99 OptStart = Specifier; 100 } 101 102 if (Diags.setDiagnosticGroupMapping(OptStart, Mapping)) 103 Diags.Report(FullSourceLoc(), diag::warn_unknown_warning_option) 104 << ("-W" + Opt); 105 } 106 107 return false; 108} 109