Warnings.cpp revision 4aa8f2bce0f498152d624f748712a991adc23fdd
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// Command line warning options handler.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
14c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar// This file is responsible for handling all warning options. This includes
150979c805475d1ba49b5d6ef93c4d2ce6d2eab6edDouglas Gregor// a number of -Wfoo options and their variants, which are driven by TableGen-
16a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner// generated data, and the special cases -pedantic, -pedantic-errors, -w,
172eadfb638eb1bb6ccfd6fd0453e764d47e27eed9Chris Lattner// -Werror and -Wfatal-errors.
18a4d55d89c8076b402bb168e3edeef0c2cd2a78c3Chris Lattner//
1998cd599ee8a9b259ed7388ee2921a20d97658864Douglas Gregor// Each warning option controls any number of actual warnings.
20aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor// Given a warning option 'foo', the following are valid:
2125d0a0f67d9e949ffbfc57bf487012f5cbfd886eDouglas Gregor//    -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
2219cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson//
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Frontend/Utils.h"
2408f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Basic/Diagnostic.h"
2508f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Sema/SemaDiagnostic.h"
267a614d8380297fcd2bc23986241905d97222948cRichard Smith#include "clang/Lex/LexDiagnostic.h"
271b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Frontend/DiagnosticOptions.h"
2808f92e3a5dead1f1ee656678a7f06e43279d6e50Chris Lattner#include "clang/Frontend/FrontendDiagnostic.h"
29da5a6b6d9fd52899499d5b7b46273ec844dcaa6eChris Lattner#include <cstring>
30cf3293eaeb3853d12cff47e648bbe835004e929fDouglas Gregor#include <utility>
313a082d81006e7a2e01a6e431a22e21c78490ff8fAnders Carlsson#include <algorithm>
32ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregorusing namespace clang;
3364f45a24b19eb89ff88f7c3ff0df9be8e861ac97Eli Friedman
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                  const DiagnosticOptions &Opts) {
368d852e35adb46e0799538dfc9c80d44f27cd3597Rafael Espindola  Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
37632fbaa22fbed7c090eb83775731bfff786c2198Rafael Espindola  Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
380b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  Diags.setShowOverloads(
390b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola    static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads));
400b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
410b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  // Handle -ferror-limit
420b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  if (Opts.ErrorLimit)
43251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola    Diags.setErrorLimit(Opts.ErrorLimit);
44251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola  if (Opts.TemplateBacktraceLimit)
45251c449b280eb845017a6c022ed7189d17c63d49Rafael Espindola    Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
460b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola
470b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  // If -pedantic or -pedantic-errors was specified, then we want to map all
480b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  // extension diagnostics onto WARNING or ERROR unless the user has futz'd
490b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  // around with them explicitly.
500b4fe503ef00d9f8ea330850d3e3b303e9c7c876Rafael Espindola  if (Opts.PedanticErrors)
512b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
522b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  else if (Opts.Pedantic)
532b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
542b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner  else
552b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
56f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
57f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne  for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
582b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    StringRef Opt = Opts.Warnings[i];
59f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
60c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    // Check to see if this warning starts with "no-", if so, this is a negative
61f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    // form of the option.
62c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt    bool isPositive = true;
63f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (Opt.startswith("no-")) {
642b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      isPositive = false;
652de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      Opt = Opt.substr(3);
662b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    }
672b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
682b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    // Figure out how this option affects the warning.  If -Wfoo, map the
692b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    // diagnostic to a warning, if -Wno-foo, map it to ignore.
702b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
71c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt
726907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall    // -Wsystem-headers is a special case, not driven by the option table.  It
736907fbe758d23e1aec4c0a67e7b633d1d855feb4John McCall    // cannot be controlled with -Werror.
74f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    if (Opt == "system-headers") {
752b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      Diags.setSuppressSystemWarnings(!isPositive);
76c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt      continue;
77f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne    }
782b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner
792b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    // -Weverything is a special case as well.  It implicitly enables all
802de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    // warnings, including ones not explicitly in a warning group.
812de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    if (Opt == "everything") {
822de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      Diags.setEnableAllWarnings(true);
832de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      continue;
842de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    }
852de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall
862de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    // -Werror/-Wno-error is a special case, not controlled by the option table.
872de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    // It also has the "specifier" form of -Werror=foo and -Werror-foo.
882b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner    if (Opt.startswith("error")) {
89c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt      StringRef Specifier;
902de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      if (Opt.size() > 5) {  // Specifier must be present.
912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
922de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall          Diags.Report(diag::warn_unknown_warning_specifier)
932b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner            << "-Werror" << ("-W" + Opt.str());
942b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner          continue;
952b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner        }
96c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt        Specifier = Opt.substr(6);
972de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall      }
982de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall
992b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      if (Specifier.empty()) {
1002b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner        Diags.setWarningsAsErrors(isPositive);
1012b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner        continue;
102c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt      }
103f111d935722ed488144600cea5ed03a6b5069e8fPeter Collingbourne
1042b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      // Set the warning as error flag for this specifier.
1052b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      if (Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive)) {
106c302113179a1c2b1254224ea9b6f5316ceeb375cSean Hunt        Diags.Report(isPositive ? diag::warn_unknown_warning_option :
1072b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner                     diag::warn_unknown_negative_warning_option)
1082b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner          << ("-W" + Opt.str());
1092b334bb3126a67895813e49e6228dad4aec0b4d6Chris Lattner      }
11063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      continue;
11163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    }
11290e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar
11390e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar    // -Wfatal-errors is yet another special case.
11463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    if (Opt.startswith("fatal-errors")) {
11563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      StringRef Specifier;
11663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      if (Opt.size() != 12) {
11763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
11863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall          Diags.Report(diag::warn_unknown_warning_specifier)
11963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall            << "-Wfatal-errors" << ("-W" + Opt.str());
12063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall          continue;
12163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        }
12263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        Specifier = Opt.substr(13);
12363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      }
12463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
12563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      if (Specifier.empty()) {
12663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        Diags.setErrorsAsFatal(isPositive);
12763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        continue;
12863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      }
12963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
13090e25a8b2cc5d006e4ced052bcdb40c8af999456Daniel Dunbar      // Set the error as fatal flag for this specifier.
13163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      if (Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive)) {
13263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall        Diags.Report(isPositive ? diag::warn_unknown_warning_option :
13363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                     diag::warn_unknown_negative_warning_option)
13463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall          << ("-W" + Opt.str());
13563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      }
13663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      continue;
13763c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    }
13863c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall
13963c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    if (Diags.setDiagnosticGroupMapping(Opt, Mapping)) {
14063c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall      Diags.Report(isPositive ? diag::warn_unknown_warning_option :
14163c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall                   diag::warn_unknown_negative_warning_option)
14263c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall          << ("-W" + Opt.str());
14363c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall    }
14463c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall  }
14563c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall}
14663c00d7f35fa060c0a446c9df3a4402d9c7757feJohn McCall